線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1466
推到 Plurk!
推到 Facebook!

请教各位前辈,帮我看看下面这个问题,好吗?

答題得分者是:wodahs
lsh998
中階會員


發表:163
回覆:138
積分:60
註冊:2005-01-07

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-14 09:04:23 IP:219.128.xxx.xxx 未訂閱
有存储过程: create procedure inserttest8 @sno varchar(50), @name varchar(50) as insert test8(sno,name) values(@sno,@name) 现在我这样调用他: private void button1_Click(object sender, System.EventArgs e) { oleDbCommand1.CommandText="inserttest8"; oleDbCommand1.CommandType=CommandType.StoredProcedure; oleDbCommand1.Connection=oleDbConnection1; oleDbCommand1.Parameters.Add("@sno",OleDbType.VarChar,50).Value=textBox1.Text; oleDbCommand1.Parameters.Add("@name",OleDbType.VarChar,50).Value=textBox2.Text; oleDbCommand1.ExecuteNonQuery(); } 它的提示是: D:\net\d2\Form1.cs(139): The type or namespace name 'OleDbType' could not be found (are you missing a using directive or an assembly reference?) D:\net\d2\Form1.cs(138): The type or namespace name 'OleDbType' could not be found (are you missing a using directive or an assembly reference?) 我是错在那里呢? 谢谢!
wodahs
初階會員


發表:0
回覆:33
積分:26
註冊:2004-02-26

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-14 10:03:35 IP:61.231.xxx.xxx 未訂閱
在最前面加上System.Data.OleDb Namespace 語法: using System.Data.OleDb; wodahs ====================================================================== 我是非出身科班的菜鳥,瞎子摸象,寫錯的地方,請大家包涵包涵,不要太認真 ^^ 歡迎大家批評指教,適時糾正我錯誤觀念,這樣我才會進步並學到正確的知識 ^^ ======================================================================
lsh998
中階會員


發表:163
回覆:138
積分:60
註冊:2005-01-07

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-03-14 12:09:48 IP:219.128.xxx.xxx 未訂閱
谢谢各位大哥 我原先的错误是因为没有添加 System.Data.OleDb命名空间    不还是有错误:    程序如下:    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace WindowsApplication15 {         ///  /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Data.OleDb.OleDbConnection oleDbConnection1; private System.Data.OleDb.OleDbCommand oleDbCommand1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(176, 48); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click = new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(24, 24); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(24, 64); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 2; this.textBox2.Text = ""; // // oleDbConnection1 // this.oleDbConnection1.ConnectionString = @"Integrated Security=SSPI;Packet Size=4096;Data Source=LSH;Tag with column collation when possible=False;Initial Catalog=lshtest;Use Procedure for Prepare=1;Auto Translate=True;Persist Security Info=False;Provider=""SQLOLEDB.1"";Workstation ID=LSH;Use Encryption for Data=False"; // // oleDbCommand1 // this.oleDbCommand1.Connection = this.oleDbConnection1; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { oleDbCommand1.CommandText="inserttest8"; oleDbCommand1.CommandType=CommandType.StoredProcedure; oleDbCommand1.Connection=oleDbConnection1; oleDbCommand1.Parameters.Add("@sno" ,OleDbType.VarChar,50).Value=textBox1.Text; oleDbCommand1.Parameters.Add("@name",OleDbType.VarChar,50).Value=textBox2.Text; oleDbCommand1.ExecuteNonQuery(); } } } 各位大哥现在是能够编译了 可是当我单击 button1 出现错误: 错误提示是:an unhand exception of type ‘system.invalidoperationexception’ occurred in system.data.dll additional information:executenonquery requires an open available connection. The connection’s current state id closed. 是不是错是是因为没有打开和数据库的连接呢? oleDbConnection1 是和数据库连接上了的! 各位大哥,我是错在那里呢? 谢谢!
lsh998
中階會員


發表:163
回覆:138
積分:60
註冊:2005-01-07

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-03-14 12:10:07 IP:219.128.xxx.xxx 未訂閱
谢谢各位大哥 我原先的错误是因为没有添加 System.Data.OleDb命名空间    不还是有错误:    程序如下:    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace WindowsApplication15 {         ///  /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Data.OleDb.OleDbConnection oleDbConnection1; private System.Data.OleDb.OleDbCommand oleDbCommand1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(176, 48); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click = new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(24, 24); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(24, 64); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 2; this.textBox2.Text = ""; // // oleDbConnection1 // this.oleDbConnection1.ConnectionString = @"Integrated Security=SSPI;Packet Size=4096;Data Source=LSH;Tag with column collation when possible=False;Initial Catalog=lshtest;Use Procedure for Prepare=1;Auto Translate=True;Persist Security Info=False;Provider=""SQLOLEDB.1"";Workstation ID=LSH;Use Encryption for Data=False"; // // oleDbCommand1 // this.oleDbCommand1.Connection = this.oleDbConnection1; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { oleDbCommand1.CommandText="inserttest8"; oleDbCommand1.CommandType=CommandType.StoredProcedure; oleDbCommand1.Connection=oleDbConnection1; oleDbCommand1.Parameters.Add("@sno" ,OleDbType.VarChar,50).Value=textBox1.Text; oleDbCommand1.Parameters.Add("@name",OleDbType.VarChar,50).Value=textBox2.Text; oleDbCommand1.ExecuteNonQuery(); } } } 各位大哥现在是能够编译了 可是当我单击 button1 出现错误: 错误提示是:an unhand exception of type ‘system.invalidoperationexception’ occurred in system.data.dll additional information:executenonquery requires an open available connection. The connection’s current state id closed. 是不是错是是因为没有打开和数据库的连接呢? oleDbConnection1 是和数据库连接上了的! 各位大哥,我是错在那里呢? 谢谢!
lsh998
中階會員


發表:163
回覆:138
積分:60
註冊:2005-01-07

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-03-14 12:10:28 IP:219.128.xxx.xxx 未訂閱
谢谢各位大哥 我原先的错误是因为没有添加 System.Data.OleDb命名空间    不还是有错误:    程序如下:    using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace WindowsApplication15 {         ///  /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Data.OleDb.OleDbConnection oleDbConnection1; private System.Data.OleDb.OleDbCommand oleDbCommand1; /// /// Required designer variable. /// private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.oleDbCommand1 = new System.Data.OleDb.OleDbCommand(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(176, 48); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "button1"; this.button1.Click = new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(24, 24); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(24, 64); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 2; this.textBox2.Text = ""; // // oleDbConnection1 // this.oleDbConnection1.ConnectionString = @"Integrated Security=SSPI;Packet Size=4096;Data Source=LSH;Tag with column collation when possible=False;Initial Catalog=lshtest;Use Procedure for Prepare=1;Auto Translate=True;Persist Security Info=False;Provider=""SQLOLEDB.1"";Workstation ID=LSH;Use Encryption for Data=False"; // // oleDbCommand1 // this.oleDbCommand1.Connection = this.oleDbConnection1; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { oleDbCommand1.CommandText="inserttest8"; oleDbCommand1.CommandType=CommandType.StoredProcedure; oleDbCommand1.Connection=oleDbConnection1; oleDbCommand1.Parameters.Add("@sno" ,OleDbType.VarChar,50).Value=textBox1.Text; oleDbCommand1.Parameters.Add("@name",OleDbType.VarChar,50).Value=textBox2.Text; oleDbCommand1.ExecuteNonQuery(); } } } 各位大哥现在是能够编译了 可是当我单击 button1 出现错误: 错误提示是:an unhand exception of type ‘system.invalidoperationexception’ occurred in system.data.dll additional information:executenonquery requires an open available connection. The connection’s current state id closed. 是不是错是是因为没有打开和数据库的连接呢? oleDbConnection1 是和数据库连接上了的! 各位大哥,我是错在那里呢? 谢谢!
wodahs
初階會員


發表:0
回覆:33
積分:26
註冊:2004-02-26

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-03-14 12:55:21 IP:61.220.xxx.xxx 未訂閱
應該是連接沒打開,或被其他共用Connection的操作不小心關閉. 試著在oleDbCommand1.ExecuteNonQuery();前後下達Connection的操作. if (oleDbConnection1.State != ConnectionState.Open) oleDbConnection1.Open(); oleDbCommand1.ExecuteNonQuery(); oleDbConnection1.Close(); //如果沒有其他共用Connection就可關閉 wodahs ====================================================================== 我是非出身科班的菜鳥,瞎子摸象,寫錯的地方,請大家包涵包涵,不要太認真 ^^ 歡迎大家批評指教,適時糾正我錯誤觀念,這樣我才會進步並學到正確的知識 ^^ ======================================================================
lsh998
中階會員


發表:163
回覆:138
積分:60
註冊:2005-01-07

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-03-14 15:08:22 IP:219.128.xxx.xxx 未訂閱
非常感谢 wodahs 大哥 ^_^
系統時間:2024-05-13 7:43:54
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!