使用我的應用程序,我想確保用戶是否在文本框中沒有輸入任何值,並單擊保存按鈕以在sqlserver數據庫中發送數據。數據庫端驗證可以防止這種違規行爲,並設置我的應用程序將捕獲的ErrorMessage,並向用戶顯示有意義的消息
。對於每個必填字段,我將其設置爲NOT NULL。但是當我測試時,我仍然可以輸入
輸入空值文本框值,它插入的值爲out值。 我在想什麼?如何強制用戶在數據庫字段中輸入值
string connectionstring = "Data Source=abcdef;Initial Catalog=HMS;Persist Security Info=True;User ID=sysad;Password=abcdef";
SqlConnection connection = new SqlConnection(connectionstring);
string SelectStatement = "SELECT * FROM tablename where RegistrationNo = @RegistrationNo";
SqlCommand insertcommand = new SqlCommand(SelectStatement, connection);
insertcommand.Parameters.AddWithValue("@RegistrationNo", textBox10.Text);
SqlDataReader reader;
try
{
connection.Open();
reader = insertcommand.ExecuteReader();
while (reader.Read())
{
textBox11.Text = reader["RegistrationNo"].ToString();
textBox1.Text = reader["Appearance"].ToString();
textBox2.Text = reader["VolumePH"].ToString();
textBox3.Text = reader["Mobility"].ToString();
textBox4.Text = reader["Viability"].ToString();
textBox5.Text = reader["Head"].ToString();
textBox6.Text = reader["MiddlePiece"].ToString();
textBox7.Text = reader["Tail"].ToString();
textBox8.Text = reader["SpermCount"].ToString();
dateTimePicker1.Text = reader["Date"].ToString();
textBox9.Text = reader["Comment"].ToString();
}//end while
reader.Close();
}
catch (Exception ex)
{
throw ex;
}//end catch
我想,一個空字符串被插入到數據庫中,不違反NOT NULL約束。 –
TextBox不具有NULL值,它們具有空值。空值不等於NULL,所以在數據庫上不會引發違規。 –
在Win App中,您可以通過驗證檢查空的文本框。 [這裏](http://stackoverflow.com/a/5605687/1004522)是鏈接。 –