[英]How to add a new row to an existing table using c# sql server
I need to write a program. 我需要写一个程序。 A part of the program is to write to an sql database (.mdf).
程序的一部分是写入sql数据库(.mdf)。 I had a lot of trouble trying to add a new row to my table (called: "Data").
我在尝试向表中添加新行时遇到了很多麻烦(称为“数据”)。 Here is the code:
这是代码:
...
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");
...
I execute this code, but the data didn't get saved to the table. 我执行此代码,但数据没有保存到表中。 Does anyone know how to enter a new row to the table and to save it?
有谁知道如何在表中输入新行并保存它?
You need an InsertCommand in your SqlDataAdapter. 您需要在SqlDataAdapter中使用InsertCommand 。
EDIT: 编辑:
Here's a quick example I whipped up. 这是我掀起的一个简单例子。 There are many others out there, but this should get you going.
还有很多其他的,但这应该让你去。 It assumes that you have a table (dbo.Foos) with two columns (Foo int, Bar nvarchar(50)).
它假设您有一个包含两列的表(dbo.Foos)(Foo int,Bar nvarchar(50))。
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();
}
}
}
强制数据集接受更改, ds.Acceptchanges
添加到您的代码中
Two things I'm seeing, you're not initializing your Dataset (ds) or SqlDataAdapter (da) in anyway (unless you're simply leaving that out for post simplification). 我正在看到的两件事情,你无论如何都没有初始化你的数据集(ds)或SqlDataAdapter(da)(除非你只是将它留下来进行后期简化)。 Part of the initialization of the da will be giving it an actual sql command.
da的初始化的一部分将给它一个实际的sql命令。
Try instead to set 请尝试改为设置
dRow = new DataRow();
instead of 代替
dRow = ds.Tables["Data"].NewRow();
and change 并改变
da.Update(ds, "Data");
to 至
da.Update(ds);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.