简体   繁体   中英

DataSet help in C#

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?

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.

I checked this statement in Sql Server 2008:

Select (*) from <table>

It doesn't work. I never seen this syntax, not in sqlserver 2005, nor Oracle nor 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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