我有一個程序寫入Access數據表,然後信息顯示在數據網格中。當文本框中出現一個6位數字後跟一個"L"
時,會觸發此操作。 我有"TextBox1.Clear();"
在清除6個數字的代碼,但不是"L"
。c#文本框沒有清除
這裏是我的代碼:
private void Registration_Main(object sender, KeyPressEventArgs e)
{
SqlConnection DBConnection = new SqlConnection("Data Source=DATABASE;Initial Catalog=imis;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
Object returnValue;
Object returnName;
string txtend = textBox1.Text;
if (e.KeyChar == 'L')
{
DBConnection.Open();
}
if (DBConnection.State == ConnectionState.Open)
{
if (textBox1.Text.Length != 6) return;
{
cmd.CommandText = ("SELECT last_name +', '+ first_name from name where id [email protected]");
cmd.Parameters.Add(new SqlParameter("Name", textBox1.Text.Replace(@"L", "")));
cmd.CommandType = CommandType.Text;
cmd.Connection = DBConnection;
returnValue = cmd.ExecuteScalar() + "\t " + textBox1.Text.Replace(@"L", "");
returnName = cmd.ExecuteScalar();
DBConnection.Close();
AccessDB_Connection(returnName);
}
textBox1.Clear();
}
}
private void AccessDB_Connection(object returnName)
{
String varID;
Object varName;
varID = textBox1.Text.Replace(@"L", "").ToString();
varName = returnName;
OleDbConnection OLEDB_Connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\Test Applications\\Tablet Registration - Access Database\\Tablet Registration - Access\\Tablet Registration - Access\\Registration.accdb");
OleDbCommand updateCmd = new OleDbCommand();
try
{
updateCmd.CommandText = "INSERT INTO TestDB ([Name], [ID]) VALUES (@NAME, @ID)";
updateCmd.Parameters.AddWithValue("@NAME", varName);
updateCmd.Parameters.AddWithValue("@ID", varID);
OLEDB_Connection.Open();
updateCmd.Connection = OLEDB_Connection;
updateCmd.ExecuteNonQuery();
this.testDBTableAdapter.Fill(this.registrationDataSet1.TestDB);
OLEDB_Connection.Close();
}
catch
{
}
}
任何幫助,非常感謝!
@Bhavesh我不認爲這會有什麼區別 – geedubb
@Ryan這是一個winforms應用程序? – geedubb