简体   繁体   English

C#中的DataSet帮助

[英]DataSet help in C#

I connected an sql database in c# and now trying to put the contents into a dataset. 我在c#中连接了一个sql数据库,现在尝试将内容放入数据集中。 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");" 该行:“ mySqlDataAdapter.Fill(myDataSet,” Internet_Bankaciligi“);” gives an error as 'Invalid object name 'Internet_Bankaciligi'.' 给出错误消息,例如“无效的对象名称“ Internet_Bankaciligi”。 but Internet_Bankaciligi is my database name. 但是Internet_Bankaciligi是我的数据库名称。

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);" 然后:“ SqlCommand selectCMD = new SqlCommand(“从IB_Account中选择(*)”,conn);“ gives an error saying invalid syntax. 给出错误,指出语法无效。 How will I get it correct? 我该如何正确?

If " Internet_Bankaciligi " is your actual database name, then you can't execute a SQL command directly against it. 如果“ Internet_Bankaciligi ”是您的实际数据库名称,那么您将无法直接对其执行SQL命令。 You have to change your SQL to select from a table or a view. 您必须更改SQL以从表或视图中进行选择。

Your second example doesn't work because " SELECT (*) " is not valid syntax. 您的第二个示例不起作用,因为“ SELECT (*) ”是无效的语法。 It should be " SELECT * FROM IB_Account "... no parentheses. 它应该是“ SELECT * FROM IB_Account ” ...没有括号。

I checked this statement in Sql Server 2008: 我在Sql Server 2008中检查了以下语句:

Select (*) from <table>

It doesn't work. 没用 I never seen this syntax, not in sqlserver 2005, nor Oracle nor sqlite. 我从未见过这种语法,在sqlserver 2005,Oracle或sqlite中都没有。

try this one: 试试这个:

Select * from <table>

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. 谷歌他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM