我想使用基于服务的数据库将数据插入数据库我使用Visual Studio 2010.我创建了表,但它不起作用,这里是我的代码: 我会如果您有代码如何插入到数据库的建议,请加以理解。插入数据到数据库Visual Studio
public void AddAnimal(int animalID, string name, double age, string category, string gender, string extraAnimalInfo)
{
using (SqlConnection con = new SqlConnection(DBAccessLayer.Properties.Settings.Default.AnimalDBConnectionString))
{
con.Open();
try
{
using (SqlCommand command = new SqlCommand("INSERT INTO AnimalTable VALUES(@AnimalID, @Name, @Age, @Category, @Gender, @ExtraAnimalInfo)", con))
{
Console.WriteLine("Here 4");
command.Parameters.Add(new SqlParameter("AnimalID", animalID));
command.Parameters.Add(new SqlParameter("Name", name));
command.Parameters.Add(new SqlParameter("Age", age));
command.Parameters.Add(new SqlParameter("Category", category));
command.Parameters.Add(new SqlParameter("Gender", gender));
command.Parameters.Add(new SqlParameter("ExtraAnimalInfo", extraAnimalInfo));
command.ExecuteNonQuery();
}
}
catch (Exception ex)
{
Console.WriteLine("Could not insert.");
}
}
UPDATE1: 当我将数据插入到该行受影响的数据库,但是当我从服务器资源管理器没有打开数据库表发生了变化。
当我将数据插入到该行受影响的数据库,但是当我从服务器资源管理器打开数据库表我没有看到任何改变。
定义“不工作”。抛出异常?哪个例外?什么信息与该异常相关联? –
按照Eric J.的说法,将Console.WriteLine(“Could not insert。”);改为Console.WriteLine(“Could not insert:”+ ex);'你会得到一些有用的信息你或我们可以用它来诊断正在发生的事情。 –
当我将数据插入数据库时,行受到影响,但是当我从服务器资源管理器打开数据库表时,没有任何更改。 – EM10