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); 请教各位大哥,我是错在那里呢? 谢谢!