我有這張表具有user_Id和regNo字段的配置文件,並且我希望先檢查ID和電子郵件是否已存在,然後再繼續插入數據。在我的代碼中,我只能驗證一行(id或reg number),但是如果我要驗證它們中的兩個,它會給我一個錯誤,說「必須聲明標量變量@用戶名」。我不知道它是否與我的選擇是錯誤的或我的代碼中的東西。檢查ID和REG是否已經存在
SqlConnection con = new SqlConnection("Data Source=GATE-PC\\SQLEXPRESS;Initial Catalog=dbProfile;Integrated Security=True");
con.Open();
SqlCommand cmdd = new SqlCommand("select * from Profile where user_Id = @userid AND RegNo = @reg", con);
SqlParameter param = new SqlParameter();
//SqlParameter param1 = new SqlParameter();
param.ParameterName = "@userid";
param.ParameterName = "@reg";
param.Value = txtid.Text;
param.Value = txtregNo.Text;
cmdd.Parameters.Add(param);
//cmdd.Parameters.Add(param1);
SqlDataReader reader = cmdd.ExecuteReader();
if (reader.HasRows)
{
MessageBox("User Id/Registry Number already exists");
}
else
{
SqlConnection con = new SqlConnection("Data Source=GATE-PC\\SQLEXPRESS;Initial Catalog=dbProfile;Integrated Security=True");
SqlCommand cmd = new SqlCommand("qry", con);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Parameters.AddWithValue("@id", txtid.Text);
cmd.Parameters.AddWithValue("@regno", txtregNo.Text);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
con.Open();
cmd.ExecuteNonQuery();
MessageBox("successfully saved!");
}
我正在使用C#與asp.net。
您好,感謝您的回覆。我試過你的代碼建議。但它只是驗證註冊號,用戶標識未驗證,即使存儲ID也已存在 – user2388316
將'id'設置爲表中的主鍵? –
不,我的主鍵不是我的主鍵 – user2388316