2017-03-22 63 views
1

我是新來的C#和「連接字符串屬性尚未初始化」是我的問題。我搜索並嘗試了我在互聯網上看到的所有內容,但它根本無法幫助我。我不知道該怎麼辦了,請幫助:(connectionstring屬性尚未初始化;標準表達式中的數據類型不匹配;數據庫不更新

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace salon 
{ 
    public partial class Form4 : Form 
    { 
     OleDbConnection conn = new OleDbConnection(); 
     public Form4() 
     { 
      InitializeComponent(); 
      conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "/myDB.accdb"; 
     } 

     private void Form4_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'myDBDataSet.tblCustomerInfo' table. You can move, or remove it, as needed. 
      this.tblCustomerInfoTableAdapter.Fill(this.myDBDataSet.tblCustomerInfo); 

     } 


     private void btnContinue_Click(object sender, EventArgs e) 
     { 
      OleDbConnection conn = new OleDbConnection(); 



       conn.Open(); 

       OleDbCommand cmd = new OleDbCommand(); 
       cmd.CommandType = CommandType.Text; 
       cmd.CommandText = @"insert into tblCustomerInfo(CustomerName, PhoneNo, Email) values ('" + txtName.Text + "','" + txtNo.Text + "','" + txtEmail.Text + "'"; 

       cmd.Connection = conn; 

       cmd.ExecuteNonQuery(); 


       conn.Close(); 


       string date = System.DateTime.Today.ToString("ddmmyy"); 
       MessageBox.Show("Your Information: " + Environment.NewLine + "Name: " + txtName.Text + Environment.NewLine + "Phone no.: " + txtNo.Text + Environment.NewLine + "Email: " + txtEmail.Text + Environment.NewLine + Environment.NewLine + "Your chosen date is: " + timePicker.Value); 



      MessageBox.Show("Your date is successfully reserved"); 
      Form1 frm1 = new Form1(); 
      frm1.Show(); 
      this.Hide(); 

     } 


    } 
} 

請幫我這一次,我是新來的C#,我不知道如何解決這個問題。我試圖尋找和所有,但沒有運氣。@取消幸運提出參數化查詢,但我不知道怎麼樣。我試圖尋找它,沒有運氣。

data type mismatch

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; 

namespace salon 
{ 
    public partial class Form4 : Form 
    { 
     OleDbConnection conn = new OleDbConnection(); 
     public Form4() 
     { 
      InitializeComponent(); 

     } 

     private void Form4_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'myDBDataSet.tblCustomerInfo' table. You can move, or remove it, as needed. 
      this.tblCustomerInfoTableAdapter.Fill(this.myDBDataSet.tblCustomerInfo); 

     } 


     private void btnContinue_Click(object sender, EventArgs e) 
     { 

      string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "/myDB.accdb"; 
       OleDbConnection conn = new OleDbConnection(); 
      conn.ConnectionString = conString; 
       conn.Open(); 

       OleDbCommand cmd = new OleDbCommand(); 
       cmd.Connection = conn; 
       cmd.CommandType = CommandType.Text; 
       cmd.CommandText = @"insert into tblCustomerInfo(CustomerName, PhoneNo, Email) values ('" + txtName.Text + "','" + txtNo.Text + "','" + txtEmail.Text + "')"; 
       cmd.Parameters.AddWithValue("@CustomerName", txtName.Text); 
       cmd.Parameters.AddWithValue("@PhoneNo", txtNo.Text); 
       cmd.Parameters.AddWithValue("@Email", txtEmail.Text); 

       cmd.ExecuteNonQuery(); 


       conn.Close(); 


       MessageBox.Show("Your Information: " + Environment.NewLine + "Name: " + txtName.Text + Environment.NewLine + "Phone no.: " + txtNo.Text + Environment.NewLine + "Email: " + txtEmail.Text + Environment.NewLine + Environment.NewLine + "Your chosen date is: " + timePicker.Value); 



      MessageBox.Show("Your date is successfully reserved"); 
      Form1 frm1 = new Form1(); 
      frm1.Show(); 
      this.Hide(); 

     } 


    } 
} 

UPDATE 我定有關的錯誤數據類型不匹配但現在當我開始調試它的數據庫沒有更新有什麼問題?請幫助

回答

1

您必須初始化OleDbCommand的ConnectionString屬性,因爲您可以使用該構造函數,或者稍後可以通過將相應的值設置爲該屬性來初始化它們。讓conString是被用來存儲連接字符串和類似下面的初始化的全局字符串變量:

string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "/myDB.accdb" 

現在你可以這樣定義

OleDbConnection conn = new OleDbConnection(conString); 

或類似這樣的連接:

OleDbConnection conn = new OleDbConnection(); 
conn.ConnectionString = conString; 

你必須注意的其他一些事情是:

  • 您在插入查詢中有語法錯誤,這意味着您錯過了查詢結尾處的右括號。
  • 其實,使用純文本查詢不是一個好的選擇,我強烈建議您改用參數。
+0

謝謝,但現在我進入另一個錯誤「INSERT INTO語句中的語法錯誤。」 – Tsuna

+0

,現在是「標準表達式中的數據類型不匹配」。它指向cmd.ExecuteNonQuery(); – Tsuna

+0

對不起,延長的問題,雖然 – Tsuna

相關問題