我在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);" 出现错误,说明语法无效.我怎样才能正确理解?
如果" Internet_Bankaciligi
"是您的实际数据库名称,则无法直接对其执行SQL命令.您必须更改SQL以从表或视图中进行选择.
您的第二个示例不起作用,因为" SELECT (*)
"不是有效的语法.它应该是" SELECT * FROM IB_Account
"......没有括号.