⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 C#2005 实例源代码
💻 CS
字号:
using System;
using System.Data;
using System.Data.SqlClient;

namespace DataSet_Fill
{
	/// <summary>
	/// 使用DataAdpapter填充DataSet
	/// </summary>
	class T
	{
		/// <summary>
		/// 使用DataAdpapter填充DataSet
		/// </summary>
		public void FillDataSet() 
		{
			//连接数据库
			SqlConnection myCon = new SqlConnection();
			myCon.ConnectionString = "Persist Security Info=False;User id=sa;pwd=sa;database=northwind;server=(local)";
			myCon.Open();

			//使用SqlCommand提交查询命令
			SqlCommand selectCMD = new SqlCommand("SELECT top 10 CustomerID, CompanyName FROM Customers", myCon);

			//获取数据适配器
			SqlDataAdapter custDA = new SqlDataAdapter();
			custDA.SelectCommand = selectCMD;

			//填充DataSet
			DataSet custDS = new DataSet();
			custDA.Fill(custDS, "Customers");
			//显示XML格式的数据
			//Console.WriteLine("{0}",custDS.GetXml());

			//显示其中的DataTable对象中的数据
			for(int i=0;i<custDS.Tables[0].Rows.Count;i++)
			{
				for(int j=0;j<custDS.Tables[0].Columns.Count;j++)
				{
					Console.Write("{0}	",custDS.Tables[0].Rows[i].ItemArray[j]);
				}
				Console.WriteLine();
			}

			//断开连接
			myCon.Close();
		}


		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			T t=new T();
			t.FillDataSet();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -