📄 class1.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 + -