2013-05-13 73 views
0

接入層:工作在C#三層,但我的插入查詢不工作

public bool AddStudent(string busStudentFullName, string busStudentFatherName) 
{ 
    con = new SqlCeConnection(); 
    con.ConnectionString = "data source = C:\\Users\\hasni\\Documents\\Visual Studio 2010\\Projects\\UniversityManagementSystem\\UniversityManagementSystem\\UniversityDB.sdf"; 
    con.Open(); 

    ds1 = new DataSet(); 
    //DataTable t = new DataTable(); 
    // string sql = "SELECT * from AdminPassword where Admin Name ='" + AdminNameLogintextBox.Text + "' and Password='" + PasswordLogintextBox.Text + "'"; 
    //string qry = "SELECT * FROM Students"; 

    // string sql = "SELECT * from AdminPassword where Admin Name ='" + AdminNameLogintextBox.Text + "' and Password='" + PasswordLogintextBox.Text + "'"; 
    string sql = "SELECT * FROM Students"; 


    da = new SqlCeDataAdapter(sql, con); 
    //da = new SqlCeDataAdapter(); 

    //DataTable t = new DataTable(); 
    //da.Fill(t); 
    da.Fill(ds1, "Students"); 


    //string userNameDB = Convert.ToString(ds1.Tables[0]); 
    // return userNameDB; 

    con.Close(); 
    // string busStudentFullName; 
    //string busStudentFatherName; 
    string sql2 = "INSERT INTO Students (Student Full Name,Student Father Name) Values('"+ busStudentFullName + "','" + busStudentFatherName + "')"; 


    da = new SqlCeDataAdapter(sql2, con); 
    da.Fill(ds1, "Students"); 

    con.Close(); 
    return true; 

} 

業務層:

public bool getResponseForAddStudent(string studentName, string studentfathername) 
{ 
    bool var = access.AddStudent(studentName, studentfathername); 
    return var; 
} 

表示層:

private void AddStudentButton_Click(object sender, EventArgs e) 
{ 
    string studentName = StudentNameBox.Text; 
    string studentfathername = StdFatherNameBox.Text; 

    bool var = _busGeneral.getResponseForLogin(studentName, studentfathername); 

    if (var) 
    { 
     MessageBox.Show("Student Added"); 
    } 
    else 
    { 
     MessageBox.Show("Sorry"); 
    } 
} 
+0

如果您的問題與您的插入查詢有關,那麼爲什麼向我們展示剩下的所有內容?而且,INSERT查詢的問題究竟是什麼?你不能指望我們猜測。 – 2013-05-13 22:07:33

+0

請格式化您的代碼,使其更具可讀性 – MethodMan 2013-05-13 22:07:50

+1

列名中的空格;正確的方法:[我的專欄名稱] – Tim 2013-05-13 22:09:17

回答

0

您的SQL ISN」 t有效,當列名稱中有空格時需用方括號括起來:
INSERT INTO Students (Student Full Name,Student Father Name)
,而不是必須
INSERT INTO Students ([Student Full Name],[Student Father Name])

0

如果我理解你正在嘗試做的,也有一些問題與除括號問題你的代碼。

首先,在執行最後一次DataAdapter.Fill操作之前關閉連接。 而且由於您希望在用學生數據填充DataAdapter之前插入記錄,您必須首先使用SqlCeCommand對象發出ExecuteNonQuery語句。此外,爲避免注入攻擊和其他問題,您應始終使用參數化查詢。我還建議用try ... catch來處理錯誤代碼。

以下是我認爲你正在努力實現與插入操作(我只有案頭檢查語法):

// con.Close(); 
// string busStudentFullName; 
SqlCeCommand cmd = db.CreateCommand(); 
cmd.CommandText = "INSERT INTO Students ([Student Full Name],[Student Father Name]) Values(@FullName, @DadsName)"; 
cmd.AddParameter("@FullName", busStudentFullName); 
cmd.AddParameter("@DadsName", busStudentFatherName); 
cmd.ExecuteNonQuery(); 

在這一點上,你可以填寫與學生行包括新DataAdapter的插入一個。

+0

Sir im im使用相同的格式,因爲我正在做它的更新和比較,但它沒有插入。 – 2013-05-14 07:49:29