【发布时间】:2013-02-08 03:56:09
【问题描述】:
我真的不知道为什么我会收到这条消息
ExecuteNonQuery:CommandText 属性尚未初始化
我的代码完全没有错误,而且当我查看它时它看起来不错。我一直在网上寻找解决方案,但找不到任何解决方案。这是我的代码。非常感谢任何帮助。
SqlConnection conn = new SqlConnection("Data Source=VALONS;Initial Catalog=gym;Integrated Security=True");
SqlCommand cmd;
public NewMem()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void NewMem_Load(object sender, EventArgs e)
{
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
conn.Open();
cmd = new SqlCommand("Insert into member(SocialSecurity, Name, City, Street, Zipcode, Email, Phone) values ('" + textSocialSecurity + "','" + textName + "','" + textCity + "','" + textStreet + "','" + textZipCode + "','" + textEmail + "','" + textPhone + "')" + conn);
cmd.ExecuteNonQuery();
MessageBox.Show("You have successfully inserted values in Member");
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
【问题讨论】:
-
您知道您的代码存在名为 sql injection 的安全问题吗?
-
也许是 ", conn" 而不是 "+ conn"?
-
当 O'Reilly 先生输入他的名字时会发生什么?您需要为此使用 SqlParameters。
标签: c#