【问题标题】:How to add a new row to an existing table using c# sql server如何使用 c# sql server 向现有表中添加新行
【发布时间】:2009-11-24 16:03:01
【问题描述】:

我需要编写一个程序。该程序的一部分是写入 sql 数据库 (.mdf)。 尝试向表中添加新行(称为:“数据”)时遇到了很多麻烦。代码如下:

...
DataSet ds = new DataSet();
System.Data.SqlClient.SqlDataAdapter da;
DataRow dRow;
string sql = "SELECT * From Data";
da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
...
System.Data.SqlClient.SqlCommandBuilder cb;
cb = new System.Data.SqlClient.SqlCommandBuilder(da);
dRow = ds.Tables["Data"].NewRow();
dRow[0] = "my_data1";
dRow[1] = "my_data2";
dRow[2] = "my_data3";
...
ds.Tables["Data"].Rows.Add(dRow);
da.Update(ds, "Data");
...

我执行了这段代码,但数据没有保存到表中。有谁知道如何在表格中输入新行并保存它?

【问题讨论】:

    标签: c# sql database


    【解决方案1】:

    您的 SqlDataAdapter 中需要一个 InsertCommand

    编辑:

    这是我快速编写的一个示例。那里还有很多其他人,但这应该能让你继续前进。它假定您有一个包含两列 (Foo int, Bar nvarchar(50)) 的表 (dbo.Foos)。

    namespace DataAdapterSample
    {
        using System;
        using System.Data;
        using System.Data.SqlClient;
    
        class Program
        {
            static void Main(string[] args)
            {
                using (SqlConnection connection = new SqlConnection(@"Data Source=[your server];Initial Catalog=[your database];Integrated Security=true;"))
                {
                    using (SqlDataAdapter dataAdapter = new SqlDataAdapter())
                    {
                        dataAdapter.SelectCommand = new SqlCommand("select Foo, Bar from dbo.Foos", connection);
                        dataAdapter.InsertCommand = new SqlCommand("insert into dbo.Foos (Foo, Bar) values (@Foo, @Bar)", connection);
                        dataAdapter.InsertCommand.Parameters.Add(new SqlParameter("Foo", SqlDbType.Int, 4, "Foo"));
                        dataAdapter.InsertCommand.Parameters.Add(new SqlParameter("Bar", SqlDbType.NText, 50, "Bar"));
    
                        using (DataSet dataSet = new DataSet())
                        {
                            dataAdapter.Fill(dataSet);
    
                            Console.WriteLine("There are {0} rows in the table", dataSet.Tables[0].Rows.Count);
    
                            DataRow newRow = dataSet.Tables[0].NewRow();
                            newRow["Foo"] = 5;
                            newRow["Bar"] = "Hello World!";
                            dataSet.Tables[0].Rows.Add(newRow);
    
                            dataAdapter.Update(dataSet);
                        }                
    
                        //Just to prove we inserted
                        using (DataSet newDataSet = new DataSet())
                        {
                            dataAdapter.Fill(newDataSet);
                            Console.WriteLine("There are {0} rows in the table", newDataSet.Tables[0].Rows.Count);                
                        }                
                    }
                }
                Console.ReadLine();        
            }
        }
    }
    

    【讨论】:

    • 感谢您的回答。你能给我写一个样本吗,因为我尝试使用“InsertCommand”,但我失败了。再次感谢!
    【解决方案2】:

    强制数据集接受更改,即将ds.Acceptchanges 添加到您的代码中

    【讨论】:

      【解决方案3】:

      我看到了两件事,无论如何,您都没有初始化 Dataset (ds) 或 SqlDataAdapter (da)(除非您只是为了简化而将其忽略)。 da 的部分初始化将给它一个实际的 sql 命令。

      【讨论】:

      • 他确实初始化了它:string sql = "SELECT * From Data"; da = new System.Data.SqlClient.SqlDataAdapter(sql, con);他只需要一个 InsertCommand 到 da
      【解决方案4】:

      尝试改为设置

      dRow = new DataRow();
      

      而不是

      dRow = ds.Tables["Data"].NewRow();
      

      改变

      da.Update(ds, "Data");
      

      da.Update(ds);
      

      【讨论】:

      • -1 DataRow 类有一个内部构造函数。您不能在表格之外创建一个
      猜你喜欢
      • 1970-01-01
      • 2010-10-24
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      相关资源
      最近更新 更多