c# 从数据库读取数据

2024-11-25 20:01:59
推荐回答(1个)
回答1:

//如下是将DB的数据放到DataTable
public DataTable SelSQL(String SQL)
{
DataTable Table = new DataTable();
SqlConnection myCon = new SqlConnection(LinkStr); //Linestr为连接路径
SqlCommand myCom = new SqlCommand(SQL, myCon);
myCom.CommandTimeout = 0;
SqlDataAdapter Adapter = new SqlDataAdapter(myCom);
try
{
Adapter.Fill(Table);
return Table;
}
catch (SqlException e)
{
MessageBox.Show(e.Message);
return Table;
}
finally
{
myCom.Dispose();
}
}

//在调用的时候再定义一个DataTable
//最后再将数据一笔笔指派个excel栏位即可