Skip to main content
 首页 » 编程设计

c#之数据库中没有发生数据插入

2024年02月23日10zfyouxi

我正在尝试将数据插入数据库。连接工作正常。但是我无法插入数据,我也不知道为什么。该表只是没有得到更新。

string ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Kaushalya\\Documents\\NewAgain.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; 
 
SqlCommand cmd = new SqlCommand("Insert into Tabel1(name) VALUES (@UserName);",conn); 
cmd.Parameters.Add(new SqlParameter("@UserName", Convert.ToInt32(0))); 

测试时连接正常。但是,我无法对数据库执行任何操作。

请您参考如下方法:

您需要执行命令对象。

cmd.ExecuteNonQuery(); 

更多信息 here .