ご協力いただきありがとうございます。みなさん(文字通り)
エントリを更新しているコードの別の部分を確認し、そのコードを使用して変更しました。今それは動作します
ここにあります
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.SqlClient.SqlException'の未処理の例外がSystem.Data.dllで発生しました追加情報:'='の近くの構文が正しくありません。
これが私のコードです:
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();
}