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

请教各位大哥,我下面的程序是 使 dataGrid1 显示数据,可是有错误啊~~!

尚未結案
lsh998
中階會員


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

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-16 17:47:28 IP:219.128.xxx.xxx 未訂閱
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; using System.Data.OleDb; using System.Data.SqlTypes; using System.Data.Common;    namespace WindowsApplication1 {         ///  /// 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; private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; private System.Data.OleDb.OleDbCommand oleDbInsertCommand1; private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; private System.Data.SqlClient.SqlCommand sqlCommand1; private System.Data.SqlClient.SqlCommand sqlSelectCommand1; private System.Data.SqlClient.SqlCommand sqlInsertCommand1; private System.Data.SqlClient.SqlCommand sqlUpdateCommand1; private System.Data.SqlClient.SqlCommand sqlDeleteCommand1; private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; private System.Windows.Forms.DataGrid dataGrid1; private System.Data.DataSet dataSet1; private System.Data.SqlClient.SqlConnection sqlConnection1; private System.Windows.Forms.Button button2; /// /// 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.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbInsertCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbUpdateCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbDeleteCommand1 = new System.Data.OleDb.OleDbCommand(); this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(); this.sqlCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(); this.dataGrid1 = new System.Windows.Forms.DataGrid(); this.dataSet1 = new System.Data.DataSet(); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.button2 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(216, 32); 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(16, 16); this.textBox1.Name = "textBox1"; this.textBox1.TabIndex = 1; this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(16, 48); this.textBox2.Name = "textBox2"; this.textBox2.TabIndex = 2; this.textBox2.Text = ""; // // oleDbDataAdapter1 // this.oleDbDataAdapter1.DeleteCommand = this.oleDbDeleteCommand1; this.oleDbDataAdapter1.InsertCommand = this.oleDbInsertCommand1; this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1; this.oleDbDataAdapter1.UpdateCommand = this.oleDbUpdateCommand1; // // sqlCommand1 // this.sqlCommand1.Connection = this.sqlConnection1; // // sqlDataAdapter1 // this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1; this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1; this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1; this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1; // // dataGrid1 // this.dataGrid1.DataMember = ""; this.dataGrid1.DataSource = this.dataSet1; this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dataGrid1.Location = new System.Drawing.Point(16, 80); this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.TabIndex = 3; // // dataSet1 // this.dataSet1.DataSetName = "NewDataSet"; this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN"); // // sqlConnection1 // this.sqlConnection1.ConnectionString = "workstation id=LSH;packet size=4096;integrated security=SSPI;data source=\"LSH\\LSH" "2004\";persist security info=False;initial catalog=lshtest"; this.sqlConnection1.InfoMessage = new System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage); // // button2 // this.button2.Location = new System.Drawing.Point(216, 72); this.button2.Name = "button2"; this.button2.TabIndex = 4; this.button2.Text = "button2"; this.button2.Click = new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(312, 273); this.Controls.Add(this.button2); this.Controls.Add(this.dataGrid1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit(); 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) { sqlCommand1.CommandText="procinserttest1"; sqlCommand1.CommandType=CommandType.StoredProcedure; sqlCommand1.Connection=sqlConnection1; sqlCommand1.Parameters.Add("@sno" ,SqlDbType.VarChar,50).Value=textBox1.Text; sqlCommand1.Parameters.Add("@name" ,SqlDbType.VarChar,50).Value=textBox2.Text; if (sqlConnection1.State != ConnectionState.Open) sqlConnection1.Open(); sqlCommand1.ExecuteNonQuery(); sqlConnection1.Close(); } private void button2_Click(object sender, System.EventArgs e) { DataTable dt = new DataTable(); DataSet ds = new DataSet(); sqlCommand1.CommandText="procselecttest1"; sqlCommand1.CommandType=CommandType.StoredProcedure; sqlCommand1.Connection=sqlConnection1; sqlCommand1.Parameters.Add("@sno" ,SqlDbType.VarChar,50).Value=textBox1.Text; if (sqlConnection1.State != ConnectionState.Open) sqlConnection1.Open(); SqlDataAdapter da = new SqlDataAdapter(sqlCommand1); da.Fill(ds); dt = ds.Tables[0]; this.dataGrid1.DataSource=dt.DefaultView; this.dataGrid1.DataBind(); sqlConnection1.Close(); } private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e) { } } } 错误在:this.dataGrid1.DataBind(); 错误提示是: C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\WindowsApplication1\Form1.cs(234): 'System.Windows.Forms.DataGrid' does not contain a definition for 'DataBind' 如果我写 this.dataGrid1.DataBind(); 这一句的话,能编译,但是连续单击 button2 就有错误,那么错误在 :da.Fill(ds); 请教各位大哥,我是错在那里呢? 谢谢!
wodahs
初階會員


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-18 02:12:36 IP:61.62.xxx.xxx 未訂閱
您似乎沒寫上當錯誤在da.fill(ds)時,畫面顯示的錯誤什麼... 個人猜測可能是重入問題,一般可用lock或monitor方式可以解決, 但就本例而言,可簡單嘗試於button2 click event handler裏將 connection操作刪去(因DataAdapter內部會操作Connection,故此 例中其為多餘程式碼).可能可以解決. wodahs ====================================================================== 我是非出身科班的菜鳥,瞎子摸象,寫錯的地方,請大家包涵包涵,不要太認真 ^^ 歡迎大家批評指教,適時糾正我錯誤觀念,這樣我才會進步並學到正確的知識 ^^ ======================================================================
lsh998
中階會員


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

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