C# 是否将数据保存到表中不工作?
在我下面的代码中,我调用了一个方法并引用了一个列表,以保存每个对象的所有数据,如姓名、年龄、性别和信息,但是在我测试了我的应用程序之后,表是空的!我是否错过了什么或做错了什么?我没有错C# 是否将数据保存到表中不工作?,c#,sql,C#,Sql,在我下面的代码中,我调用了一个方法并引用了一个列表,以保存每个对象的所有数据,如姓名、年龄、性别和信息,但是在我测试了我的应用程序之后,表是空的!我是否错过了什么或做错了什么?我没有错 public void SaveDataToDB(List<Animal> animals) { connection = new SqlConnection(connectionString); dataset = new DataSet();
public void SaveDataToDB(List<Animal> animals)
{
connection = new SqlConnection(connectionString);
dataset = new DataSet();
sql = "SELECT * From Guests";
try
{
connection.Open();
adapter = new SqlDataAdapter(sql, connection);
adapter.Fill(dataset, "Guests");
foreach (Animal animal in animals)
{
DataRow row = dataset.Tables["Guests"].NewRow();
row["Name"] = animal.Name;
row["Age"] = animal.Age;
row["Gender"] = animal.Gender;
row["Info"] = animal.ImportantInfo;
dataset.Tables["Guests"].Rows.Add(row);
}
new SqlCommandBuilder(adapter);
adapter.Update(dataset.Tables["Guests"]);
connection.Close();
}
catch
{
throw;
}
}
public void SaveDataToDB(列出动物)
{
连接=新的SqlConnection(connectionString);
数据集=新数据集();
sql=“从来宾中选择*”;
尝试
{
connection.Open();
适配器=新的SqlDataAdapter(sql,连接);
adapter.Fill(数据集,“来宾”);
foreach(动物中的动物)
{
DataRow row=dataset.Tables[“来宾”].NewRow();
行[“名称”]=动物名称;
行[“年龄”]=动物年龄;
行[“性别”]=动物性别;
行[“信息”]=animal.ImportantInfo;
dataset.Tables[“Guests”].Rows.Add(row);
}
新的SqlCommandBuilder(适配器);
adapter.Update(dataset.Tables[“Guests”]);
connection.Close();
}
抓住
{
投掷;
}
}
要使插入正确工作,您需要为适配器定义一个插入命令。以下是示例:
public void SaveDataToDB(List<Animal> animals)
{
SqlConnection connection = new SqlConnection(connectionString);
DataSet dataset = new DataSet();
string sql = "SELECT * From Guests";
try
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
adapter.Fill(dataset, "Guests");
// Create the InsertCommand.
SqlCommand command = new SqlCommand(
"INSERT INTO Guests (Name, Age, Gender, ImportantInfo) " +
"VALUES (@Name, @Age, @Gender, @ImportantInfo)", connection);
// Add the parameters for the InsertCommand.
command.Parameters.Add("@Name", SqlDbType.NVarChar, 50, "Name");
command.Parameters.Add("@Age", SqlDbType.Int, 4, "Age");
command.Parameters.Add("@Gender", SqlDbType.NVarChar, 6, "Gender");
command.Parameters.Add("@ImportantInfo", SqlDbType.NVarChar, 100, "ImportantInfo");
foreach (Animal animal in animals)
{
DataRow row = dataset.Tables["Guests"].NewRow();
row["Name"] = animal.Name;
row["Age"] = animal.Age;
row["Gender"] = animal.Gender;
row["Info"] = animal.ImportantInfo;
dataset.Tables["Guests"].Rows.Add(row);
}
new SqlCommandBuilder(adapter);
adapter.Update(dataset.Tables["Guests"]);
connection.Close();
}
catch
{
throw;
}
}
public void SaveDataToDB(列出动物)
{
SqlConnection连接=新的SqlConnection(connectionString);
数据集=新数据集();
string sql=“从来宾中选择*”;
尝试
{
connection.Open();
SqlDataAdapter=新的SqlDataAdapter(sql,连接);
adapter.Fill(数据集,“来宾”);
//创建InsertCommand。
SqlCommand=newsqlcommand(
在来宾中插入(姓名、年龄、性别、重要信息)+
“值(@Name、@Age、@Gender、@ImportantInfo)”,连接);
//添加InsertCommand的参数。
Add(“@Name”,SqlDbType.NVarChar,50,“Name”);
Add(“@Age”,SqlDbType.Int,4,“Age”);
Add(“@Gender”,SqlDbType.NVarChar,6,“Gender”);
Add(“@ImportantInfo”,SqlDbType.NVarChar,100,“ImportantInfo”);
foreach(动物中的动物)
{
DataRow row=dataset.Tables[“来宾”].NewRow();
行[“名称”]=动物名称;
行[“年龄”]=动物年龄;
行[“性别”]=动物性别;
行[“信息”]=animal.ImportantInfo;
dataset.Tables[“Guests”].Rows.Add(row);
}
新的SqlCommandBuilder(适配器);
adapter.Update(dataset.Tables[“Guests”]);
connection.Close();
}
抓住
{
投掷;
}
}
确保为db参数指定实际的db类型和大小。要使插入正确工作,您需要为适配器定义一个InsertCommand
。以下是示例:
public void SaveDataToDB(List<Animal> animals)
{
SqlConnection connection = new SqlConnection(connectionString);
DataSet dataset = new DataSet();
string sql = "SELECT * From Guests";
try
{
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
adapter.Fill(dataset, "Guests");
// Create the InsertCommand.
SqlCommand command = new SqlCommand(
"INSERT INTO Guests (Name, Age, Gender, ImportantInfo) " +
"VALUES (@Name, @Age, @Gender, @ImportantInfo)", connection);
// Add the parameters for the InsertCommand.
command.Parameters.Add("@Name", SqlDbType.NVarChar, 50, "Name");
command.Parameters.Add("@Age", SqlDbType.Int, 4, "Age");
command.Parameters.Add("@Gender", SqlDbType.NVarChar, 6, "Gender");
command.Parameters.Add("@ImportantInfo", SqlDbType.NVarChar, 100, "ImportantInfo");
foreach (Animal animal in animals)
{
DataRow row = dataset.Tables["Guests"].NewRow();
row["Name"] = animal.Name;
row["Age"] = animal.Age;
row["Gender"] = animal.Gender;
row["Info"] = animal.ImportantInfo;
dataset.Tables["Guests"].Rows.Add(row);
}
new SqlCommandBuilder(adapter);
adapter.Update(dataset.Tables["Guests"]);
connection.Close();
}
catch
{
throw;
}
}
public void SaveDataToDB(列出动物)
{
SqlConnection连接=新的SqlConnection(connectionString);
数据集=新数据集();
string sql=“从来宾中选择*”;
尝试
{
connection.Open();
SqlDataAdapter=新的SqlDataAdapter(sql,连接);
adapter.Fill(数据集,“来宾”);
//创建InsertCommand。
SqlCommand=newsqlcommand(
在来宾中插入(姓名、年龄、性别、重要信息)+
“值(@Name、@Age、@Gender、@ImportantInfo)”,连接);
//添加InsertCommand的参数。
Add(“@Name”,SqlDbType.NVarChar,50,“Name”);
Add(“@Age”,SqlDbType.Int,4,“Age”);
Add(“@Gender”,SqlDbType.NVarChar,6,“Gender”);
Add(“@ImportantInfo”,SqlDbType.NVarChar,100,“ImportantInfo”);
foreach(动物中的动物)
{
DataRow row=dataset.Tables[“来宾”].NewRow();
行[“名称”]=动物名称;
行[“年龄”]=动物年龄;
行[“性别”]=动物性别;
行[“信息”]=animal.ImportantInfo;
dataset.Tables[“Guests”].Rows.Add(row);
}
新的SqlCommandBuilder(适配器);
adapter.Update(dataset.Tables[“Guests”]);
connection.Close();
}
抓住
{
投掷;
}
}
确保为db参数指定实际的db类型和大小。可能没有错误,因为您在catch子句中重新引用了错误。删除try..catch并查看是否有错误。请发布来宾表定义。很抱歉,这是一个愚蠢的问题,但我如何获得它?可能您没有收到错误,因为您正在重新引用catch子句中的错误。删除try..catch并查看是否有错误。请发布来宾表定义。很抱歉提出了一个愚蠢的问题,但我如何获得它?这不只是更新内存中的数据集吗?不是实际的数据库表?我通常不使用SqlDataAdapter进行插入,只选择,所以我真的不确定…@Mark,我也不确定,所以我刚刚在本地测试了它,这段代码也更新了数据库。太棒了!我得考虑未来的发展!我唯一不喜欢的是将SQL硬编码到应用程序中。我倾向于使用存储过程!好像我的代码在工作!谢谢Andrei提供的代码,但这些都不是必需的。我正在使用的tha应用程序的要点和目标是在断开连接的模式下使用数据库,作为要标记的注释。我现在的问题是为什么列表在使用这个方法的过程中丢失了所有的数据。列表中没有项目=数据库中没有要保存的内容!:)这不是只更新内存中的数据集吗?不是实际的数据库表?我通常不使用SqlDataAdapter进行插入,只选择,所以我真的不确定…@Mark,我也不确定,所以我刚刚在本地测试了它,这段代码是upda