C#中的SQL数据集 [英] Data Set With SQL in C#
本文介绍了C#中的SQL数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据集调用SetItemType并且它包含两个数据表
01)ITEM_GROUP
02)ITEM_TYPE
我想从sql数据库添加价值到这个数据表
如下:
I Have a Data Set Call SetItemType And It Contain Two Data Table
01) ITEM_GROUP
02) ITEM_TYPE
I Want To add Value from sql Data base To This Data Table
like followings
SetItemType type = new SetItemType();
type.ITEM_GROUP = "SQL Statement";
如何做到这一点?
how to do this ?
推荐答案
你可以用这种方式。 ..我提到了storeproceduresp_test,它返回了超过1个表,然后你可以用下面的方式使用这个表...
you can use this way...here i mention storeprocedure "sp_test" which returns more then 1 tables then you can used this tables in below way...
public DataTable GetData()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConString"].ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand("sp_test", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter MyDataAdapter = new SqlDataAdapter();
DataSet DS = new DataSet();
try
{
MyDataAdapter.SelectCommand = cmd;
MyDataAdapter.Fill(DS);
if (DS != null && DS.Tables.Count > 0 && DS.Tables[0].Rows.Count > 0)
{
return DS.Tables[0];
}
else
{
return null;
}
}
catch (SqlException )
{
throw ;
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn = null;
}
}
可能会对你有帮助...谢谢你,如果你得到你的答案那么请回答...
may be this can help you...Thanks if you get your answer then please make as answered...
试试这个...
try {
connection.Open();
command = new SqlCommand(firstSql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds, "First Table");
adapter.SelectCommand.CommandText = secondSql;
adapter.Fill(ds, "Second Table");
adapter.Dispose();
command.Dispose();
connection.Close();
//retrieve first table data
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) {
Interaction.MsgBox(ds.Tables[0].Rows[i][0] + " -- " + ds.Tables[0].Rows[i][1]);
}
//retrieve second table data
for (i = 0; i <= ds.Tables[1].Rows.Count - 1; i++) {
Interaction.MsgBox(ds.Tables[1].Rows[i][0] + " -- " + ds.Tables[1].Rows[i][1]);
}
}
catch (Exception ex)
{
Interaction.MsgBox("");
}
这篇关于C#中的SQL数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文