在C#中怎样把sql查询的结果存放到datatable里?困扰我好久了,希望高手能把代码写出来 万分感谢
for(inti=0;i<dt.rows.count;i++)ye[i]=dt.Rows[i]["SQYE"].ToString()类似这种形式的...
for(int i=0;i<dt.rows.count;i++)
ye[i] = dt.Rows[i]["SQYE"].ToString()
类似这种形式的 展开
ye[i] = dt.Rows[i]["SQYE"].ToString()
类似这种形式的 展开
6个回答
展开全部
这个很简单, 按照这个语句写ado就行了
调用这个函数时
建一个SqlDataAdapter
SqlCommand comm = new SqlCommand("select * from table where ...");
SqlDataAdapter ad = new SqlDataAdapter(comm);
public int MyExecuteSql(SqlDataAdapter sqlAd, ref DataTable returnTable)
{
try
{
this.openConn();
sqlAd.SelectCommand.Connection = this.conn;
DataSet ds = new DataSet();
sqlAd.Fill(ds);
returnTable = ds.Tables[0];
if (ds.Tables[0].Rows.Count > 0)
{
return 1;
}
else
{
return 0;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
//return 0;
}
希望能有帮助
调用这个函数时
建一个SqlDataAdapter
SqlCommand comm = new SqlCommand("select * from table where ...");
SqlDataAdapter ad = new SqlDataAdapter(comm);
public int MyExecuteSql(SqlDataAdapter sqlAd, ref DataTable returnTable)
{
try
{
this.openConn();
sqlAd.SelectCommand.Connection = this.conn;
DataSet ds = new DataSet();
sqlAd.Fill(ds);
returnTable = ds.Tables[0];
if (ds.Tables[0].Rows.Count > 0)
{
return 1;
}
else
{
return 0;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
//return 0;
}
希望能有帮助
展开全部
先引用System.Data;System.Data.SqlClient。
代码如下:
string strConn = "Server=dbIP; database=dbname; uid=id; pwd=pwd;";
string strSql = "select score from student";
SqlDataAdapter da = new SqlDataAdapter(strSql, strConn);
DataTable dt = new DataTable();
da.Fill(dt); //获取到的内容填充到DataTable中
代码如下:
string strConn = "Server=dbIP; database=dbname; uid=id; pwd=pwd;";
string strSql = "select score from student";
SqlDataAdapter da = new SqlDataAdapter(strSql, strConn);
DataTable dt = new DataTable();
da.Fill(dt); //获取到的内容填充到DataTable中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先引入datatable所在命名空间 using System.Data;
DataSet ds = new DataSet();
using (OracleConnection connection = new OracleConnection(connectionString))
{
//create a command and prepare it for execution
using (OracleCommand cmd = new OracleCommand())
{
try
{
//prepare command for execution
PrepareCommand(cmd, connection, null, sqlText, cmdParms);
//create the DataAdapter
OracleDataAdapter da = new OracleDataAdapter(cmd);
//fill the DataSet using default values for DataTable names, etc.
da.Fill(ds, "ds");
// detach the OracleParameters from the command object, so they can be used again.
cmd.Parameters.Clear();
return ds.table[0];最后一句返回了datatable.
DataSet ds = new DataSet();
using (OracleConnection connection = new OracleConnection(connectionString))
{
//create a command and prepare it for execution
using (OracleCommand cmd = new OracleCommand())
{
try
{
//prepare command for execution
PrepareCommand(cmd, connection, null, sqlText, cmdParms);
//create the DataAdapter
OracleDataAdapter da = new OracleDataAdapter(cmd);
//fill the DataSet using default values for DataTable names, etc.
da.Fill(ds, "ds");
// detach the OracleParameters from the command object, so they can be used again.
cmd.Parameters.Clear();
return ds.table[0];最后一句返回了datatable.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
OracleConnection thisConnection = new OracleConnection(@"Data Source=orcl;User ID=shibei;Password=nsic;Unicode=True");
// thisConnection.Open();//此处可以不用打开
OracleDataAdapter thisAdapter = new OracleDataAdapter("select * from N_AIT", thisConnection);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "MY_N_AIT");//MY_N_AIT不是表名而是DataTable对象的名称
DataTable dt = thisDataSet .Tables[0];
// thisConnection.Open();//此处可以不用打开
OracleDataAdapter thisAdapter = new OracleDataAdapter("select * from N_AIT", thisConnection);
DataSet thisDataSet = new DataSet();
thisAdapter.Fill(thisDataSet, "MY_N_AIT");//MY_N_AIT不是表名而是DataTable对象的名称
DataTable dt = thisDataSet .Tables[0];
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接用SqlDataAdapter填充DataTable也可以的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询