c# - 如何使用 c# sql server 向现有表添加新行

标签 c# sql database

我需要写一个程序。该程序的一部分是写入 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");
...

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

最佳答案

你需要一个 InsertCommand在您的 SqlDataAdapter 中。

编辑:

这是我快速创建的一个示例。还有很多其他人在那里,但这应该让你去。它假定您有一个包含两列(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();        
        }
    }
}

关于c# - 如何使用 c# sql server 向现有表添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1791042/

相关文章:

javascript - 使用 C# 在 PhantomJS 中启用 JavaScript

c# - 使用 Entity Framework 在 SQL 表中查找所有重复记录

c# - 无法使用 Entity Framework 回滚事务

java - 在 MySQL 数据库中使用 JDBC 更新数据

c# - OLEDB - 无法编辑 Access 数据库中的记录

C#.NET 正则表达式未按预期工作

SQL查询将一个表中的值插入到另一个表中

java - 将数据从文本表传输到普通表 hsqldb java

日期范围查询的 SQL 索引

sql-server - 反复将数据从 SQL Server 移动到 Oracle