C# 中的数据集帮助

发布于 2024-08-02 21:36:09 字数 1415 浏览 9 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羅雙樹 2024-08-09 21:36:09

如果“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.

葮薆情 2024-08-09 21:36:09

我在Sql Server 2008中检查了这个语句:

Select (*) from <table>

它不起作用。我从未见过这种语法,在 sqlserver 2005 中没有见过,在 Oracle 或 sqlite 中也没有见过。

尝试这个:

Select * from <table>

编辑:如果我是你,我会尝试使用强类型数据集,甚至实体框架,它们都更先进、更简单一起工作。谷歌他们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文