谢谢你的帮助。你们所有人(字面意思)
我检查了我正在更新条目的代码的另一部分,我使用了该代码并对其进行了修改。现在它可以工作了
这里是
string sql = "DELETE from Login WHERE UserName = '" + comboBox1.SelectedItem.ToString() + "'";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.Add(new SqlParameter("@UserName", comboBox1.SelectedItem.ToString()));
int rowdel = cmd.ExecuteNonQuery();
MessageBox.Show("Done!");
这是我从通过组合框选择的数据库中删除特定行的代码。我的老师使用了相同的代码并且他的程序有效。但是它显示错误:
" int rowInserted = cmd.ExecuteNonQuery(); "
它说
System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常附加信息:“=”附近的语法不正确。
这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
string connectionString = "Server = HP-PC\\SQLExpress; Database = CProject; Trusted_Connection = True";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connectionString;
conn.Open();
string sql = "delete from [Login] where UserName = " + comboBox1.SelectedText.ToString();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
int rowInserted = cmd.ExecuteNonQuery();
label7.Text = rowInserted.ToString();
conn.Close();
}
private void AddDeleteUsers_Load(object sender, EventArgs e)
{
string connectionstring = "Server=HP-PC\\SQLExpress;Database=CProject;Trusted_Connection=True;";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = connectionstring;
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select UserName from Login";
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
comboBox1.Items.Add(reader["UserName"].ToString());
reader.Close();
conn.Close();
}