目前我正面臨一個非常奇怪的問題,在我的c#& asp.net web OOP學習。 我已經編寫了插入數據到Microsoft Access的代碼,無論我如何嘗試數據將永遠不會插入到我的數據庫中,雖然代碼是正確的。無法插入數據沒有任何錯誤
不知何故,在其他項目中,我用同樣的方法做的工作是... 希望有人可以在這裏幫助。謝謝。
using System.Data.OleDb;
public class DataBaseCon
{
private static OleDbConnection GetConn()
{
string ConString;
ConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\user\Desktop\YogaDB.accdb";
return new OleDbConnection(ConString);
}
public static void Register(string un, string pw, DateTime dob, int ye, string hissue, string email, string contact)
{
OleDbConnection myConnection = GetConn();
string myQuery = "INSERT INTO User(Username, Password, DateOfBirth, YogaExp, HealthIssue, Email, Contact, UserType) VALUES ('" + un + "' , '" + pw + "' , '" + dob + "' , '" + ye + "', '" + hissue + "' , '" + email + "', '" + contact + "', 'student')";
OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine("Exception in DBHandler", ex);
}
finally
{
myConnection.Close();
}
}
}
按鈕功能在這裏:
protected void RegisterBtn_Click(object sender, EventArgs e)
{
string un = TextBox1.Text;
string pw = TextBox2.Text;
DateTime dob = Calendar1.SelectedDate;
int ye = int.Parse(TextBox4.Text);
string hissue = TextBox5.Text;
string email = TextBox6.Text;
string contact = TextBox7.Text;
DataBaseCon.Register(un, pw, dob, ye, hissue, email, contact);
Label8.Text = "Congratulation "+ un +" , success to register!";
}
你有什麼異常? – Pikoh
**警告** yoru代碼極易受sql注入攻擊。 –
有什麼異常,錯誤?你有沒有調試註冊方法? – Badiparmagi