c# 我想从数据库返回一个DataSet或者Datatale应该怎么写?

2024-12-18 12:40:35
推荐回答(3个)
回答1:

操作sql数据库的。如果是access,换成 oledb

string sConnectionString= "数据库连接";
SqlConnection objConn= new SqlConnection(sConnectionString);
objConn.Open();
SqlDataAdapter da= new SqlDataAdapter("sql语句", objConn);
DataSet ds = new DataSet();
da.Fill(ds);
objConn.Close();

回答2:

亲,用Command对象创建DataAdapter对象,再用DataAdapter的Fill方法将CommandText的查询结果填充到DataSet中。代码示例如下:
SqlCommand cmd = new SqlCommand();
cmd.CommandText ="select * from table_admin";
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet db = new DataSet();
adp.Fill(db,"MyTable");

回答3:

string sql = "你的语句";
string connection = "你的连接字符串";
DataSet resultData = new DataSet();
using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connection))
{
try
{
adapter.Fill(resultData);
}
catch (Exception)
{
}
}
//resultData已经获取过你要的结果了