偶刚看C#,在绑定ComboBox 时老不成功,浏览数据表时完成正确,但页面就是不显示数据

linden28 2003-10-17 11:00:30
以下是源代码,请大家帮看看问题出现在哪里?是还没执行查询还是没打开数据库连接,还是。。。先谢了。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication
{

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.SqlClient.SqlCommand sqlCommand1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private WindowsApplication.DataSet1 dataSet11;
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.ComponentModel.Container components = null;

public Form1()
{InitializeComponent();}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码

private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.dataSet11 = new WindowsApplication.DataSet1();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
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.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit();
this.SuspendLayout();

this.comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedValue", this.dataSet11, "PA_SERVICE.SERVICE_CODE"));
this.comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("SelectedItem", this.dataSet11, "PA_SERVICE.SERVICE_NAME"));
this.comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("ValueMember", this.dataSet11, "PA_SERVICE.SERVICE_CODE"));
this.comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("DisplayMember", this.dataSet11, "PA_SERVICE.SERVICE_NAME"));
this.comboBox1.DataSource = this.dataSet11.PA_SERVICE;
this.comboBox1.DisplayMember = "SERVICE_NAME";
this.comboBox1.Location = new System.Drawing.Point(88, 48);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 0;
this.comboBox1.ValueMember = "SERVICE_CODE";

this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN");

this.sqlConnection1.ConnectionString = "workstation id=LINQUANBAO;packet size=4096;user id=sa;data source=LINQUANBAO;pers" +
"ist security info=False;initial catalog=PA";
this.sqlConnection1.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage);

this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
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", "PA_SERVICE", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("SERVICE_CODE", "SERVICE_CODE"),
new System.Data.Common.DataColumnMapping("SERVICE_NAME", "SERVICE_NAME")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;

this.sqlSelectCommand1.CommandText = "SELECT SERVICE_CODE, SERVICE_NAME FROM PA_SERVICE";
this.sqlSelectCommand1.Connection = this.sqlConnection1;

this.sqlInsertCommand1.CommandText = "INSERT INTO PA_SERVICE(SERVICE_CODE, SERVICE_NAME) VALUES (@SERVICE_CODE, @SERVIC" +
"E_NAME); SELECT SERVICE_CODE, SERVICE_NAME FROM PA_SERVICE WHERE (SERVICE_CODE =" +
" @SERVICE_CODE)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SERVICE_CODE", System.Data.SqlDbType.VarChar, 30, "SERVICE_CODE"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SERVICE_NAME", System.Data.SqlDbType.VarChar, 80, "SERVICE_NAME"));

this.sqlUpdateCommand1.CommandText = @"UPDATE PA_SERVICE SET SERVICE_CODE = @SERVICE_CODE, SERVICE_NAME = @SERVICE_NAME WHERE (SERVICE_CODE = @Original_SERVICE_CODE) AND (SERVICE_NAME = @Original_SERVICE_NAME OR @Original_SERVICE_NAME IS NULL AND SERVICE_NAME IS NULL); SELECT SERVICE_CODE, SERVICE_NAME FROM PA_SERVICE WHERE (SERVICE_CODE = @SERVICE_CODE)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SERVICE_CODE", System.Data.SqlDbType.VarChar, 30, "SERVICE_CODE"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@SERVICE_NAME", System.Data.SqlDbType.VarChar, 80, "SERVICE_NAME"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_SERVICE_CODE", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SERVICE_CODE", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_SERVICE_NAME", System.Data.SqlDbType.VarChar, 80, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SERVICE_NAME", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.CommandText = "DELETE FROM PA_SERVICE WHERE (SERVICE_CODE = @Original_SERVICE_CODE) AND (SERVICE" +
"_NAME = @Original_SERVICE_NAME OR @Original_SERVICE_NAME IS NULL AND SERVICE_NAM" +
"E IS NULL)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_SERVICE_CODE", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SERVICE_CODE", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_SERVICE_NAME", System.Data.SqlDbType.VarChar, 80, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "SERVICE_NAME", System.Data.DataRowVersion.Original, null));
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
this.ResumeLayout(false);
}
#endregion


[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
{ }

}}


...全文
80 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
linden28 2003-10-17
  • 打赏
  • 举报
回复
谢谢两位:meetweb(niky) ,zhyl_yg(勇敢者)
zhyl_yg 2003-10-17
  • 打赏
  • 举报
回复
在form_load里写上

sqlDataAdapter1.Fill(dataSet11);
comboBox1.DataSource = dataSet11.Tables[0];
comboBox1.DisplayMember = "ProductName";

meetweb 2003-10-17
  • 打赏
  • 举报
回复
在form_load里写上
DataSet DataSet1=new DataSet();
sqlDataAdapter1.Fill(DataSet1);
ComboBox1.DataSource = DataSet1.Tables("Suppliers")
ComboBox1.DisplayMember = "ProductName"

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧