C# 中的数据集帮助
我在 C# 中连接了一个 SQL 数据库,现在尝试将内容放入数据集中。我怎样才能做到这一点?
我的代码是:
string constr = "Data Source=ECEE;Initial Catalog=Internet_Bankaciligi;User ID=sa";
SqlConnection conn = new SqlConnection(constr);
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("Select * from Internet_Bankaciligi", conn);
DataSet myDataSet = new DataSet();
DataRow myDataRow;
SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
mySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
mySqlDataAdapter.Fill(myDataSet,"Internet_Bankaciligi");
myDataRow = myDataSet.Tables["IB_Account"].NewRow();
myDataRow["Account_ID"] = "NewID";
myDataRow["Branch_ID"] = "New Branch";
myDataRow["Amount"] = "New Amount";
myDataSet.Tables["Customers"].Rows.Add(myDataRow);
行:“mySqlDataAdapter.Fill(myDataSet,"Internet_Bankaciligi");”给出错误“对象名称‘Internet_Bankaciligi’无效。”但 Internet_Bankaciligi 是我的数据库名称。
另外,如果我使用:
SqlCommand selectCMD = new SqlCommand("select (*) from IB_Account", conn);
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = selectCMD;
myAdapter.Fill(myDataSet);
那么:“SqlCommand selectCMD = new SqlCommand("select (*) from IB_Account", conn);"给出一个错误,指出语法无效。我怎样才能得到正确的结果?
I connected an sql database in c# and now trying to put the contents into a dataset. How will I be able to do that?
My code is:
string constr = "Data Source=ECEE;Initial Catalog=Internet_Bankaciligi;User ID=sa";
SqlConnection conn = new SqlConnection(constr);
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter("Select * from Internet_Bankaciligi", conn);
DataSet myDataSet = new DataSet();
DataRow myDataRow;
SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(mySqlDataAdapter);
mySqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
mySqlDataAdapter.Fill(myDataSet,"Internet_Bankaciligi");
myDataRow = myDataSet.Tables["IB_Account"].NewRow();
myDataRow["Account_ID"] = "NewID";
myDataRow["Branch_ID"] = "New Branch";
myDataRow["Amount"] = "New Amount";
myDataSet.Tables["Customers"].Rows.Add(myDataRow);
the line: "mySqlDataAdapter.Fill(myDataSet,"Internet_Bankaciligi");" gives an error as 'Invalid object name 'Internet_Bankaciligi'.' but Internet_Bankaciligi is my database name.
Also if i use:
SqlCommand selectCMD = new SqlCommand("select (*) from IB_Account", conn);
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = selectCMD;
myAdapter.Fill(myDataSet);
then: "SqlCommand selectCMD = new SqlCommand("select (*) from IB_Account", conn);" gives an error saying invalid syntax. How will I get it correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果“
Internet_Bankaciligi
”是您的实际数据库名称,则您无法直接对其执行 SQL 命令。您必须更改 SQL 才能从表或视图中进行选择。您的第二个示例不起作用,因为“
SELECT (*)
”不是有效的语法。它应该是“SELECT * FROM IB_Account
”...没有括号。If "
Internet_Bankaciligi
" is your actual database name, then you can't execute a SQL command directly against it. You have to change your SQL to select from a table or a view.Your second example doesn't work because "
SELECT (*)
" is not valid syntax. It should be "SELECT * FROM IB_Account
"... no parentheses.我在Sql Server 2008中检查了这个语句:
它不起作用。我从未见过这种语法,在 sqlserver 2005 中没有见过,在 Oracle 或 sqlite 中也没有见过。
尝试这个:
编辑:如果我是你,我会尝试使用强类型数据集,甚至实体框架,它们都更先进、更简单一起工作。谷歌他们。
I checked this statement in Sql Server 2008:
It doesn't work. I never seen this syntax, not in sqlserver 2005, nor Oracle nor sqlite.
try this one:
Edit: If I were you I will try using strongly typed datasets, or even Entity Framework which both are more advance and easier to work with. Google them.