我有一個數據庫的問題我在c#中使用winform時,我通過表單創建名稱列中的數據之前創建一個空間,所以我想刪除它,任何人都可以幫我 name column has數據類型爲varchar而接觸不具有數字類型就不會創造空間之前winform C#使用sql server 2008
我的代碼:
private void btnAddNew_Click(object sender, EventArgs e)
{
string Gender = "";
if (radioButton1.Checked)
{
Gender = "Male";
}
else if (radioButton2.Checked)
{
Gender = "Female";
}
if (txtName.Text == "")
{
MessageBox.Show("Enter the customer name.", "Dairy Management System", MessageBoxButtons.OK, MessageBoxIcon.Stop);
txtName.Focus();
}
else if (Gender == "")
{
MessageBox.Show("Enter the gender.", "Dairy Management System", MessageBoxButtons.OK, MessageBoxIcon.Stop);
grpGender.Focus();
}
else if (txtAddress.Text == "")
{
MessageBox.Show("Enter the Address.", "Dairy Management System", MessageBoxButtons.OK, MessageBoxIcon.Stop);
txtAddress.Focus();
}
else if (txtContact.Text == "")
{
MessageBox.Show("Enter Contact No.", "Dairy Management System", MessageBoxButtons.OK, MessageBoxIcon.Stop);
txtContact.Focus();
}
else
{
SqlConnection con = new SqlConnection("Data Source=RANJEETMAURYA;Initial Catalog=Project;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO CustomerDetails
(Date, Contact_No, Name, Gender, Address, Email_ID)
VALUES(' " + this.dateTimePicker1.Value.ToString("MM/dd/yyyy") + " ',' " + txtName.Text + " ',' " + Gender + " ',' " + txtAddress.Text + " ',' " + txtContact.Text + " ',' " + txtEmail.Text + " ')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Customer Information Added Successfully.", "Dairy Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
SQLFunctions.Refresh(this.dataGridView1);
clear();
}
}
可以顯示的代碼,你試過嗎?表單中的輸入是否已獲得空間,或者您認爲它是按照您的說法添加的? – owen79
如果你能展示一個例子,可能會有所幫助。它不清楚你的意思。 – Fred
[SQL注入警報](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你應該**不**連接你的SQL語句 - 使用**參數化查詢**,而不是爲了避免SQL注入 –