我无法将数据输入数据库表,也没有显示任何错误。 [英] i am having trouble entering data into database table, and it is also not showing any error.
本文介绍了我无法将数据输入数据库表,也没有显示任何错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void btnaddname_Click(object sender, EventArgs e)
{
string connstring = "server=xyz\\sqlexpress;Database=myapp;Integrated Security=True";
SqlConnection con = new SqlConnection(connstring);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand sqlCmd = new SqlCommand(@"INSERT INTO Table_Name(Name) VALUES (@name",con);
sqlCmd.Parameters.Add("@name", SqlDbType.VarChar, 25).Value = textBoxname.Text;
con.Close();
}
推荐答案
试试这样..
Try like this..
string connstring = "server=xyz\\sqlexpress;Database=myapp;Integrated Security=True";
SqlConnection con = new SqlConnection(connstring);
con.Open();
SqlCommand sqlCmd = new SqlCommand(@"INSERT INTO Table_Name(Name) VALUES ('" + textBoxname.Text + "')");
sqlCmd.Connection = con;
sqlCmd.ExecuteNonQuery();
con.Close();
0)您的查询不正确,您没有正确关闭(在结尾处缺少右括号)
0) Your query is incorrect, you didn't close that properly(missing closing parenthesis at end)
SqlCommand sqlCmd = new SqlCommand(@"INSERT INTO Table_Name(Name) VALUES (@name)",con);
1)你忘了执行,缺少线下。
1) You forgot to execute, below line is missing.
sqlCmd.ExecuteNonQuery();
2)不要使用硬编码连接字符串。从配置文件中获取这些详细信息检查下面的样本
如何从C#中的App.Config获取连接字符串 [ ^ ]
3)不要对列使用保留关键字。使用有意义的列名,如Emp_Name(而不是Name)
查看更多详细信息。 保留关键字 [ ^ ]
4)在使用之前了解更多信息。
2) Don't use hard-code connectionstrings. Get those details from config files. Check the below one for sample
How to get Connection String from App.Config in C#[^]
3) Don't use Reserved keywords for columns. Use meaningful column names like Emp_Name(instead of Name)
Check it out for more details. Reserved Keywords[^]
4) Learn more before use things.
这篇关于我无法将数据输入数据库表,也没有显示任何错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文