2013-01-12 34 views
6

mới đến C#. Hãy trợ giúp!C# - ExecuteNonQuery yêu cầu kết nối mở và có sẵn. Trạng thái hiện tại của kết nối bị đóng

Tôi tiếp tục gặp lỗi sau: "ExecuteNonQuery yêu cầu kết nối mở và có sẵn. Trạng thái hiện tại của kết nối đã bị đóng". Tôi cũng không thể chèn vào cơ sở dữ liệu của mình.

Dưới đây là mã của tôi:

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 AzureSecureStore 
{ 
    public partial class Client : Form 
    { 
     OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb; Persist Security Info=False;"); 
     //OleDbConnection vcon = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb"); 
     public Client() 
     { 
      InitializeComponent(); 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

     } 

     private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 

     } 

     private void button5_Click(object sender, EventArgs e) 
     { 

     } 

     private void button4_Click(object sender, EventArgs e) 
     { 

     } 

     private void Client_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'azcureSecureStore_DatabaseDataSet5.Client' table. You can move, or remove it, as needed. 
      this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet5.Client); 
      // TODO: This line of code loads data into the 'azcureSecureStore_DatabaseDataSet2.Client' table. You can move, or remove it, as needed. 
      //this.clientTableAdapter.Fill(this.azcureSecureStore_DatabaseDataSet2.Client); 

     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      string ab = string.Format("insert into Client values({0}, '{1}', '{2}', {3}, {4}, '{5}')", 
    textBox1.Text, textBox2.Text, int.Parse(textBox3.Text), int.Parse(textBox4.Text), textBox9.Text, int.Parse(textBox10.Text)); 
      OleDbCommand vcom = new OleDbCommand(ab, vcon); 
      vcom.ExecuteNonQuery(); 
      MessageBox.Show("Data stored successfully"); 
      vcom.Dispose(); 
     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 

     } 
    } 
} 

Trả lời

7

bạn quên để mở kết nối,

vcon.Open(); 
vcom.ExecuteNonQuery(); 

nhưng hãy nhớ sử dụng

  • using tuyên bố - để xử lý đúng đối tượng
  • try-catch bl Ock - để bắt đúng trường hợp ngoại lệ (xử lý ngoại lệ)

CẬP NHẬT 1

string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SB18\Documents\Visual Studio 2010\Projects\AzureSecureStore\AzureSecureStore\AzcureSecureStore Database.accdb; Persist Security Info=False;"; 
string query = "INSERT INTO Client VALUES(@col1,@col2,@col3,@col4,@col5,@col6)"; 
using (OleDbConnection conn = new OleDbConnection(connStr)) 
{ 
    using (OleDbCommand comm = new OleDbCommand()) 
    { 
     comm.Connection = conn; 
     comm.CommandText = query; 
     comm.CommandType = CommandType.Text; 
     comm.Parameters.AddWithValue("@col1", textBox1.Text); 
     comm.Parameters.AddWithValue("@col2", textBox2.Text); 
     comm.Parameters.AddWithValue("@col3", int.Parse(textBox3.Text)); 
     comm.Parameters.AddWithValue("@col4", int.Parse(textBox4.Text)); 
     comm.Parameters.AddWithValue("@col5", textBox9.Text); 
     comm.Parameters.AddWithValue("@col6", int.Parse(textBox10.Text)); 
     try 
     { 
      conn.Open(); 
      comm.ExecuteNonQuery(); 
      MessageBox.Show("Data stored successfully"); 
     } 
     catch(OleDbException e) 
     { 
      MessageBox.Show(e.ToString()); 
     } 
    } 
} 
+0

Hi, tôi đã làm những gì bạn nói nhưng tôi nhận được lỗi sau: "OleDbException là unhandled." Không có giá trị nào được cung cấp cho một hoặc nhiều thông số. Tôi nên làm gì? – user1971823

+0

xem câu trả lời cập nhật của tôi. –

+0

Um, tôi đã thực hiện các thay đổi. Nhưng dữ liệu vẫn không được đưa vào cơ sở dữ liệu của tôi. Không chính xác lỗi nhưng tôi nhận được cửa sổ bật lên sau đây khi tôi nhấp vào chèn: "Systems.Windows.Forms.MouseEventArgs" – user1971823

2

Đối với kết nối với cơ sở dữ liệu bạn cần mở c onnection nên sử dụng

vcon.Open(); 

và sau đó

vcom.ExecuteNonQuery(); 
+0

Xin chào, tôi đã làm những gì bạn đề cập nhưng tôi nhận được lỗi sau: "OleDbException đã được giải quyết." Không có giá trị nào được cung cấp cho một hoặc nhiều thông số. Tôi nên làm gì? – user1971823

Các vấn đề liên quan