我这里有一个查询的代码,但是运行时就出现错误,请大家帮着看看!谢谢了
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace nnn
{
/// <summary>
/// Form3 的摘要说明。
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private nnn.DataSet2 dataSet21;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form3()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.sqlDataAdapter1.Fill(this.dataSet21,"wj");
this.dataGrid1.DataSource=this.dataSet21;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.label1 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.dataSet21 = new nnn.DataSet2();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet21)).BeginInit();
this.SuspendLayout();
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=ZJ;packet size=4096;integrated security=SSPI;initial catalog=dms;p" +
"ersist security info=False";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "wj", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("Chinese Name", "Chinese Name"),
new System.Data.Common.DataColumnMapping("English Name", "English Name"),
new System.Data.Common.DataColumnMapping("Health Dangers", "Health Dangers"),
new System.Data.Common.DataColumnMapping("Emergency Freatment Measures", "Emergency Freatment Measures"),
new System.Data.Common.DataColumnMapping("Leaking Emergency Measures", "Leaking Emergency Measures")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT ID, [Chinese Name], [English Name], [Health Dangers], [Emergency Freatment" +
" Measures], [Leaking Emergency Measures] FROM wj";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = @"INSERT INTO wj([Chinese Name], [English Name], [Health Dangers], [Emergency Freatment Measures], [Leaking Emergency Measures]) VALUES (@Param1, @Param2, @Param3, @Param4, @Param5); SELECT ID, [Chinese Name], [English Name], [Health Dangers], [Emergency Freatment Measures], [Leaking Emergency Measures] FROM wj";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param1", System.Data.SqlDbType.VarChar, 50, "Chinese Name"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param2", System.Data.SqlDbType.VarChar, 80, "English Name"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param3", System.Data.SqlDbType.VarChar, 600, "Health Dangers"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param4", System.Data.SqlDbType.VarChar, 600, "Emergency Freatment Measures"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param5", System.Data.SqlDbType.VarChar, 1000, "Leaking Emergency Measures"));
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.DataSource = this.dataSet21.wj;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 80);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(488, 216);
this.dataGrid1.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 1;
this.label1.Text = "查询条件";
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"Chinese Name",
"English Name"});
this.comboBox1.Location = new System.Drawing.Point(88, 24);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(96, 20);
this.comboBox1.TabIndex = 2;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// label2
//
this.label2.Location = new System.Drawing.Point(200, 24);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 3;
this.label2.Text = "label2";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(264, 24);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(104, 21);
this.textBox1.TabIndex = 4;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(392, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 24);
this.button1.TabIndex = 5;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dataSet21
//
this.dataSet21.DataSetName = "DataSet2";
this.dataSet21.Locale = new System.Globalization.CultureInfo("zh-CN");
//
// Form3
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(488, 302);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form3";
this.Text = "Form3";
this.Load += new System.EventHandler(this.Form3_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataSet21)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void Form3_Load(object sender, System.EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
try
{
this.dataSet21.Clear();
string StrSQL="SELECT * FROM wj WHERE ";
StrSQL+=this.comboBox1.Text+" LIKE '";
StrSQL+=this.textBox1.Text+"'";
//if(this.comboBox1.Text=="All")
//StrSQL="SELECT * FROM wj";
this.sqlDataAdapter1.SelectCommand.CommandText=StrSQL;
this.sqlDataAdapter1.SelectCommand.Connection=this.sqlConnection1;
//打开数据库连接
this.sqlConnection1.Open();
//执行SQL命令
this.sqlDataAdapter1.SelectCommand.ExecuteNonQuery();
//关闭连接
this.sqlConnection1.Close();
//更新数据库
this.sqlDataAdapter1.Fill(this.dataSet21,"wj");
this.dataGrid1.DataSource=this.dataSet21;
}
catch(Exception Err)
{
MessageBox.Show("查询数据库记录操作失败:"+Err.Message,"信息提示",
MessageBoxButtons.OK,MessageBoxIcon.Information);
//如果打开了连接,则关闭它
if(this.sqlConnection1.State==ConnectionState.Open)
{
this.sqlConnection1.Close();
}
}
}
}
}