Fill: SelectCommand.Connection 属性尚未初始化。错误

BIRD72sky 2010-10-25 04:47:43
用aspNetPager分页控件的时候写了一个返回DataSet的方法,现在报这个错误,整了半天也解决不了,郁闷死了,麻烦哪位大侠给看一下:
“/WebUI”应用程序中的服务器错误。
--------------------------------------------------------------------------------

ConnectionString 属性尚未初始化。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化。

源错误:


行 64: SqlDataAdapter sda = new SqlDataAdapter(safeSql, connection);
行 65: DataSet ds = new DataSet();
行 66: sda.Fill(ds);
行 67: connection.Close();
行 68: return ds;


源文件: E:\项目\Demo\CeshiAspNetPager\CeshiAspNetPager.DAL\DBHelper.cs 行: 66


我写的方法如下:
public static DataSet GetDataSet01(string safeSql)
{

//connection.Open();

// SqlCommand cmd = new SqlCommand(safeSql, connection);
// SqlDataAdapter da = new SqlDataAdapter(cmd);
// DataSet ds = new DataSet();
// da.Fill(ds);
// connection.Close();
//connection.Dispose();
// return ds;

SqlDataAdapter sda = new SqlDataAdapter(safeSql, connection);
DataSet ds = new DataSet();
sda.Fill(ds); connection.Close();
return ds;

}

说明:不管是用注释掉的方法还是用下边的方法,都报以上错误,如果有connection.Open()这句代码,则又报以下错误

“/WebUI”应用程序中的服务器错误。
--------------------------------------------------------------------------------

ConnectionString 属性尚未初始化。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: ConnectionString 属性尚未初始化。

源错误:


行 61: //connection.Dispose();
行 62: // return ds;
行 63: connection.Open(); 行 64: SqlDataAdapter sda = new SqlDataAdapter(safeSql, connection);
行 65: DataSet ds = new DataSet();


源文件: E:\项目\Demo\CeshiAspNetPager\CeshiAspNetPager.DAL\DBHelper.cs 行: 63



头疼死了,折腾一天,拜托各位了!
...全文
1505 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
clren_yesterday 2011-07-04
  • 打赏
  • 举报
回复
connection 是空的!connectionstring赋值是空的
BIRD72sky 2010-11-01
  • 打赏
  • 举报
回复
解决了,办法在我的网摘,有需要就看看吧,谢谢各位了!
BIRD72sky 2010-10-26
  • 打赏
  • 举报
回复
断点调试过,大家说的方法都试 了一遍,还是不行!
虎虎0369 2010-10-25
  • 打赏
  • 举报
回复
sda.SelectCommand = cmd; 应该是缺这句 你试试
wuyq11 2010-10-25
  • 打赏
  • 举报
回复
检查SqlConnection 的数据库连接字符串
wwfgu00ing 2010-10-25
  • 打赏
  • 举报
回复
断点调试一下啊
BIRD72sky 2010-10-25
  • 打赏
  • 举报
回复
我只是贴了两个方法,我的connection是单独写的,这个链接没有问题!
酷儿 2010-10-25
  • 打赏
  • 举报
回复
你的ConnectionString呢 晕死
SqlConnection conn = new SqlConnection(connStr); 就是这句代码
Snowdust 2010-10-25
  • 打赏
  • 举报
回复
问题已经很清楚了,connection的ConnectionString属性是空的,调试的时候看看ConnectionString属性是什么。
1、SqlConnection类   构造函数:SqlConnection(connstr);   属性:  Database//获取当前数据库或连接打开后要使用的数据库的名称        Connectionstring//获取或设置用于打开 SQL Server 数据库的字符串   方法:  Open()        Close()        Dispose()//释放所有资源         2、SqlCommand类     构造函数:SqlCommand(string,conn) 属性:  CommandType//获取或设置一个值,该值指示如何解释 CommandText 属         CommandText //获取或设置要对数据源执行SQL 语句         Connection//获取或设置 SqlCommand 的此实例使用的 SqlConnection         Parameters//获取 SqlParameterCollection。参数集合 方法:  ExecuteNonQuery(): 返回受影响函数,如增、删、改操作;         ExecuteScalar():执行查询,返回首行首列的结果;         ExecuteReader():返回一个数据流(SqlDataReader对象)。   实例1: SqlCommand cmd = new SqlCommand () cmd.connection =conn; cmd.CommandType = CommandType.Text; cmd.CommandText ="select *from produce=@ID"; cmd.Parameters.Add("@ID",SqlDBType.NVarChar,10,ID).values=1; cmd.ExecuteScalar();   实例2:   SqlCommand cmd = new SqlCommand (“select *from test”,conn);   cmd.ExecuteScalar(); 注意: ExecuteNonQuery()方法主要用户更新数据,通常它使用Update,Insert,Delete语句来操作数据库,其方法返回值意义:对于 Update,Insert,Delete 语句 执行成功是返回值为该命令所影响的行数,如果影响的行数为0时返回的值为0,如果数据操作回滚得话返回值为-1,对于这种更新操作 用我们平时所用的是否大于0的判断操作应该没有问题而且比较好,但是对于其他的操作如对数据库结构的操作,如果操作成功时返回的却是-1,这种情况跟我们平时的思维方式有点差距所以应该好好的注意了, 3、SqlDataReader类   是由ExecuteReader()返回一个数据流(SqlDataReader对象)没有构造方法   属性Connection//获取与 SqlDataReader 关联的 SqlConnection。      FieldCount//获取当前行中的列数。      HasRows//获取一个值,该值指示 SqlDataReader 是否包含一行或多行      RecordsAffect//获取执行 Transact-SQL 语句所更改、插入或删除的行数   方法:Read();//使 SqlDataReader 前进到下一条记录      GetType();//获取当前实例的 Type      NextResult();//当读取批处理 Transact-SQL 语句的结果时,使数据读取器前进到下一个结果 4、SqlDataApater类   构造方法:SqlDataApater()        SqlDataAdapter(SqlCommand)//初始化 SqlDataAdapter 类的新实例,用指定的 SqlCommand 作为 SelectCommand属性。        SqlDataApater(string,conn)//使用 SelectCommand 和 SqlConnection 对象初始化 SqlDataAdapter 类的一个新实例   属性:  DeleteCommand        SelectCommand        InsertCommand        UpdataCommand              方法:  Fill(DataSet)//在 DataSet 中添加或刷新行        Fill(DataTable)//在 DataSet 的指定范围中添加或刷新行,以与使用 DataTable 名称的数据源中的行匹配。 (继承自DbDataAdapter。        Update(DataRow[])//通过为 DataSet 中的指定数组中的每个已插入、已更新或已删除的行执行相应的 INSERT、UPDATE 或 DELETE 语句来更新数据库中的值        Update(DataSet)//通过为指定的 DataTable 中的每个已插入、已更新或已删除的行执行相应的 INSERT、UPDATE 或 DELETE 语句来更新数据库中的值        Update(DataTable) 实例1   // 隐藏了SqlCommand对象的定义,同时隐藏了SqlCommand对象与SqlDataAdapter对象的绑定 SqlDataAdapter myda= new SqlDataAdapter("select * from test",conn);   实例2   SqlCommand mySqlCommand = new SqlCommand();// 创建SqlCommand   mySqlCommand.CommandType = CommandType.Text;   mySqlCommand.CommandText = "select * from product";   mySqlCommand.Connection = sqlCnt;   SqlDataAdapter myDataAdapter = new SqlDataAdapter();   // 创建SqlDataAdapter   myDataAdapter.SelectCommand = mySqlCommand; // 为SqlDataAdapter对象绑定所要执行的SqlCommand对象 5、DataSet类     命名空间:System.Data.DataSet。   数据集,本地微型数据库,可以存储多张表。  //使用DataSet第一步就是将SqlDataAdapter返回的数据集(表)填充到Dataset对象中:   SqlDataAdapter myDataAdapter = new SqlDataAdapter("select * from product", sqlCnt); DataSet myDataSet = new DataSet(); // 创建DataSet myDataAdapter.Fill(myDataSet, "product"); // 将返回的数据集作为“表”填入DataSet中,表名可以与数据库真实的表名不同,并不影响后续的增、删、改等操作 //访问dataset中的数据 DataTable myTable = myDataSet.Tables["product"]; foreach (DataRow myRow in myTable.Rows) { foreach (DataColumn myColumn in myTable.Columns) { Console.WriteLine(myRow[myColumn]); //遍历表中的每个单元格 } } // 修改DataSet DataTable myTable = myDataSet.Tables["product"]; foreach (DataRow myRow in myTable.Rows) { myRow["name"] = myRow["name"] + "商品"; } // 将DataSet的修改提交至“数据库” SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataAdapter.Update(myDataSet, "product"); // 添加一行 DataRow myRow = myTable.NewRow(); myRow["name"] = "捷安特"; myRow["price"] = 13.2; //myRow["id"] = 100; id若为“自动增长”,此处可以不设置,即便设置也无效 myTable.Rows.Add(myRow); // 将DataSet的修改提交至“数据库” SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataAdapter.Update(myDataSet, "product"); // 删除第一行 DataTable myTable = myDataSet.Tables["product"]; myTable.Rows[0].Delete(); SqlCommandBuilder mySqlCommandBuilder = new SqlCommandBuilder(myDataAdapter); myDataAdapter.Update(myDataSet, "product");
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace 教务管理系统 { public class ClassInfo : 医院管理系统.ParentForm { 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.SqlConnection sqlConnection1; private 教务管理系统.DataSet1 dataSet11; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label8; private System.Windows.Forms.Label label9; private System.Windows.Forms.TextBox txt4; private System.Windows.Forms.TextBox txt5; private System.Windows.Forms.TextBox txt6; private System.Windows.Forms.TextBox txt9; private System.Windows.Forms.TextBox txt8; private System.Windows.Forms.TextBox txt7; private System.ComponentModel.IContainer components = null; public ClassInfo() { // 该调用是 Windows 窗体设计器所必需的。 InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化 } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region 设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand(); this.dataSet11 = new 教务管理系统.DataSet1(); this.label4 = new System.Windows.Forms.Label(); this.txt4 = new System.Windows.Forms.TextBox(); this.txt5 = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.txt6 = new System.Windows.Forms.TextBox(); this.txt9 = new System.Windows.Forms.TextBox(); this.label7 = new System.Windows.Forms.Label(); this.label8 = new System.Windows.Forms.Label(); this.txt8 = new System.Windows.Forms.TextBox(); this.txt7 = new System.Windows.Forms.TextBox(); this.label9 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit(); // // groupBox1 // this.groupBox1.Name = "groupBox1"; // // txt2 // this.txt2.Name = "txt2"; // // label1 // this.label1.Name = "label1"; this.label1.Text = "年级"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // btnSearch // this.btnSearch.Name = "btnSearch"; this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click); // // txt1 // this.txt1.Name = "txt1"; // // label2 // this.label2.Name = "label2"; this.label2.Text = "班级编号"; // // txt3 // this.txt3.Name = "txt3"; // // label3 // this.label3.Name = "label3"; this.label3.Text = "班级名称"; // // toolBar1 // this.toolBar1.Name = "toolBar1"; this.toolBar1.Size = new System.Drawing.Size(728, 41); // // dataGrid1 // this.dataGrid1.DataMember = "班级信息"; this.dataGrid1.DataSource = this.dataSet11; this.dataGrid1.Name = "dataGrid1"; // // groupBox2 // this.groupBox2.Controls.Add(this.txt4); this.groupBox2.Controls.Add(this.label4); this.groupBox2.Controls.Add(this.txt5); this.groupBox2.Controls.Add(this.label5); this.groupBox2.Controls.Add(this.label6); this.groupBox2.Controls.Add(this.txt6); this.groupBox2.Controls.Add(this.txt9); this.groupBox2.Controls.Add(this.label7); this.groupBox2.Controls.Add(this.label8); this.groupBox2.Controls.Add(this.txt8); this.groupBox2.Controls.Add(this.txt7); this.groupBox2.Controls.Add(this.label9); this.groupBox2.Name = "groupBox2"; // // da1 // this.da1.DeleteCommand = this.sqlDeleteCommand1; this.da1.InsertCommand = this.sqlInsertCommand1; this.da1.SelectCommand = this.sqlSelectCommand1; this.da1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "班级信息", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("班级编号", "班级编号"), new System.Data.Common.DataColumnMapping("年级", "年级"), new System.Data.Common.DataColumnMapping("班级名称", "班级名称"), new System.Data.Common.DataColumnMapping("班级简称", "班级简称"), new System.Data.Common.DataColumnMapping("人数", "人数"), new System.Data.Common.DataColumnMapping("班主任", "班主任")})}); this.da1.UpdateCommand = this.sqlUpdateCommand1; // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT 班级编号, 年级, 班级名称, 班级简称, 人数, 班主任 FROM 班级信息 WHERE (班级编号 LIKE @Param4) AND (年级 " + "LIKE @Param5) AND (班级名称 LIKE @Param6)"; this.sqlSelectCommand1.Connection = this.sqlConnection1; this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param4", System.Data.SqlDbType.VarChar, 14, "班级编号")); this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param5", System.Data.SqlDbType.VarChar, 4, "年级")); this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param6", System.Data.SqlDbType.VarChar, 30, "班级名称")); // // sqlConnection1 // this.sqlConnection1.ConnectionString = "workstation id=localhost;packet size=4096;integrated security=SSPI;data source=\"." + "\";persist security info=False;initial catalog=eisbook"; // // sqlInsertCommand1 // this.sqlInsertCommand1.CommandText = "INSERT INTO 班级信息(班级编号, 年级, 班级名称, 班级简称, 人数, 班主任) VALUES (@班级编号, @年级, @班级名称, @班级简称," + " @人数, @班主任); SELECT 班级编号, 年级, 班级名称, 班级简称, 人数, 班主任 FROM 班级信息 WHERE (班级编号 = @班级编号)" + ""; this.sqlInsertCommand1.Connection = this.sqlConnection1; this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级编号", System.Data.SqlDbType.VarChar, 14, "班级编号")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@年级", System.Data.SqlDbType.VarChar, 4, "年级")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级名称", System.Data.SqlDbType.VarChar, 30, "班级名称")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级简称", System.Data.SqlDbType.VarChar, 16, "班级简称")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@人数", System.Data.SqlDbType.Decimal, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(3)), ((System.Byte)(0)), "人数", System.Data.DataRowVersion.Current, null)); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班主任", System.Data.SqlDbType.VarChar, 8, "班主任")); // // sqlUpdateCommand1 // this.sqlUpdateCommand1.CommandText = @"UPDATE 班级信息 SET 班级编号 = @班级编号, 年级 = @年级, 班级名称 = @班级名称, 班级简称 = @班级简称, 人数 = @人数, 班主任 = @班主任 WHERE (班级编号 = @Original_班级编号) AND (人数 = @Original_人数 OR @Original_人数 IS NULL AND 人数 IS NULL) AND (年级 = @Original_年级 OR @Original_年级 IS NULL AND 年级 IS NULL) AND (班主任 = @Original_班主任 OR @Original_班主任 IS NULL AND 班主任 IS NULL) AND (班级名称 = @Original_班级名称 OR @Original_班级名称 IS NULL AND 班级名称 IS NULL) AND (班级简称 = @Original_班级简称 OR @Original_班级简称 IS NULL AND 班级简称 IS NULL); SELECT 班级编号, 年级, 班级名称, 班级简称, 人数, 班主任 FROM 班级信息 WHERE (班级编号 = @班级编号)"; this.sqlUpdateCommand1.Connection = this.sqlConnection1; this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级编号", System.Data.SqlDbType.VarChar, 14, "班级编号")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@年级", System.Data.SqlDbType.VarChar, 4, "年级")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级名称", System.Data.SqlDbType.VarChar, 30, "班级名称")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级简称", System.Data.SqlDbType.VarChar, 16, "班级简称")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@人数", System.Data.SqlDbType.Decimal, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(3)), ((System.Byte)(0)), "人数", System.Data.DataRowVersion.Current, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班主任", System.Data.SqlDbType.VarChar, 8, "班主任")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级编号", System.Data.SqlDbType.VarChar, 14, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级编号", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_人数", System.Data.SqlDbType.Decimal, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(3)), ((System.Byte)(0)), "人数", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_年级", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "年级", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班主任", System.Data.SqlDbType.VarChar, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班主任", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级名称", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级名称", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级简称", System.Data.SqlDbType.VarChar, 16, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级简称", System.Data.DataRowVersion.Original, null)); // // sqlDeleteCommand1 // this.sqlDeleteCommand1.CommandText = @"DELETE FROM 班级信息 WHERE (班级编号 = @Original_班级编号) AND (人数 = @Original_人数 OR @Original_人数 IS NULL AND 人数 IS NULL) AND (年级 = @Original_年级 OR @Original_年级 IS NULL AND 年级 IS NULL) AND (班主任 = @Original_班主任 OR @Original_班主任 IS NULL AND 班主任 IS NULL) AND (班级名称 = @Original_班级名称 OR @Original_班级名称 IS NULL AND 班级名称 IS NULL) AND (班级简称 = @Original_班级简称 OR @Original_班级简称 IS NULL AND 班级简称 IS NULL)"; this.sqlDeleteCommand1.Connection = this.sqlConnection1; this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级编号", System.Data.SqlDbType.VarChar, 14, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级编号", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_人数", System.Data.SqlDbType.Decimal, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(3)), ((System.Byte)(0)), "人数", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_年级", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "年级", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班主任", System.Data.SqlDbType.VarChar, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班主任", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级名称", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级名称", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级简称", System.Data.SqlDbType.VarChar, 16, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级简称", System.Data.DataRowVersion.Original, null)); // // dataSet11 // this.dataSet11.DataSetName = "DataSet1"; this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN"); // // label4 // this.label4.Location = new System.Drawing.Point(64, 32); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(100, 16); this.label4.TabIndex = 0; this.label4.Text = "班级编号"; // // txt4 // this.txt4.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班级编号")); this.txt4.Location = new System.Drawing.Point(64, 48); this.txt4.Name = "txt4"; this.txt4.ReadOnly = true; this.txt4.Size = new System.Drawing.Size(136, 21); this.txt4.TabIndex = 1; this.txt4.Text = ""; // // txt5 // this.txt5.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.年级")); this.txt5.Location = new System.Drawing.Point(232, 48); this.txt5.Name = "txt5"; this.txt5.ReadOnly = true; this.txt5.Size = new System.Drawing.Size(136, 21); this.txt5.TabIndex = 1; this.txt5.Text = ""; // // label5 // this.label5.Location = new System.Drawing.Point(232, 32); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(100, 16); this.label5.TabIndex = 0; this.label5.Text = "年级"; // // label6 // this.label6.Location = new System.Drawing.Point(408, 32); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(100, 16); this.label6.TabIndex = 0; this.label6.Text = "班级名称"; // // txt6 // this.txt6.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班级名称")); this.txt6.Location = new System.Drawing.Point(408, 48); this.txt6.Name = "txt6"; this.txt6.ReadOnly = true; this.txt6.Size = new System.Drawing.Size(136, 21); this.txt6.TabIndex = 1; this.txt6.Text = ""; // // txt9 // this.txt9.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班主任")); this.txt9.Location = new System.Drawing.Point(408, 96); this.txt9.Name = "txt9"; this.txt9.ReadOnly = true; this.txt9.Size = new System.Drawing.Size(136, 21); this.txt9.TabIndex = 1; this.txt9.Text = ""; // // label7 // this.label7.Location = new System.Drawing.Point(408, 80); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(100, 16); this.label7.TabIndex = 0; this.label7.Text = "班主任"; // // label8 // this.label8.Location = new System.Drawing.Point(232, 80); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(100, 16); this.label8.TabIndex = 0; this.label8.Text = "人数"; // // txt8 // this.txt8.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班级简称")); this.txt8.Location = new System.Drawing.Point(232, 96); this.txt8.Name = "txt8"; this.txt8.ReadOnly = true; this.txt8.Size = new System.Drawing.Size(136, 21); this.txt8.TabIndex = 1; this.txt8.Text = ""; // // txt7 // this.txt7.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班级简称")); this.txt7.Location = new System.Drawing.Point(64, 96); this.txt7.Name = "txt7"; this.txt7.ReadOnly = true; this.txt7.Size = new System.Drawing.Size(136, 21); this.txt7.TabIndex = 1; this.txt7.Text = ""; // // label9 // this.label9.Location = new System.Drawing.Point(64, 80); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(100, 16); this.label9.TabIndex = 0; this.label9.Text = "班级简称"; // // ClassInfo // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(728, 502); this.Name = "ClassInfo"; this.Text = "【班级信息维护】"; this.Load += new System.EventHandler(this.ClassInfo_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.groupBox2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(); } #endregion //--------创建窗体时,读入数据------- private void ClassInfo_Load(object sender, System.EventArgs e) { da1.SelectCommand.Parameters[0].Value="%"; da1.SelectCommand.Parameters[1].Size=8;//4位年份输入 da1.SelectCommand.Parameters[1].Value="%"; da1.SelectCommand.Parameters[2].Value="%"; da1.Fill(dataSet11); //设置数据导航控件 this.cmOrders=(CurrencyManager) BindingContext[dataSet11,"班级信息"]; //将基类中的数据集与派生类中的数据集连接 base.dataSet11=this.dataSet11; } //-----------根据输入,检索信息---------- private void btnSearch_Click(object sender, System.EventArgs e) { da1.SelectCommand.Parameters[0].Value="%"; da1.SelectCommand.Parameters[1].Value="%"; da1.SelectCommand.Parameters[2].Value="%"; if(txt1.Text.Trim()!="") { da1.SelectCommand.Parameters[0].Value="%"+txt1.Text.Trim()+"%"; } if(txt2.Text.Trim()!="") { da1.SelectCommand.Parameters[1].Value="%"+txt2.Text.Trim()+"%"; } if(txt3.Text.Trim()!="") { da1.SelectCommand.Parameters[2].Value="%"+txt3.Text.Trim()+"%"; } dataSet11.Clear();//刷新数据集 da1.Fill(dataSet11); } //----------重写设置控件只读属性函数---------- protected override void SetModifyMode(bool blnEdit) { base.SetModifyMode (blnEdit); txt4.ReadOnly=!blnEdit; txt5.ReadOnly=!blnEdit; txt6.ReadOnly=!blnEdit; txt7.ReadOnly=!blnEdit; txt8.ReadOnly=!blnEdit; txt9.ReadOnly=!blnEdit; } //-------重写新增记录时设置默认值函数-------- protected override void SetDefaultValue() { base.SetDefaultValue (); } //-------重写检查非空字段函数-------- protected override bool CheckNotNull() { if(txt4.Text.Trim()=="")// 班级编号不能为空 { MessageBox.Show("班级编号不能为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop); return(false); } return base.CheckNotNull (); } } }
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace 教务管理系统 { public class ClassInfo : 医院管理系统.ParentForm { 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.SqlConnection sqlConnection1; private 教务管理系统.DataSet1 dataSet11; private System.Windows.Forms.Label label4; private System.Windows.Forms.Label label5; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label8; private System.Windows.Forms.Label label9; private System.Windows.Forms.TextBox txt4; private System.Windows.Forms.TextBox txt5; private System.Windows.Forms.TextBox txt6; private System.Windows.Forms.TextBox txt9; private System.Windows.Forms.TextBox txt8; private System.Windows.Forms.TextBox txt7; private System.ComponentModel.IContainer components = null; public ClassInfo() { // 该调用是 Windows 窗体设计器所必需的。 InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化 } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region 设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand(); this.dataSet11 = new 教务管理系统.DataSet1(); this.label4 = new System.Windows.Forms.Label(); this.txt4 = new System.Windows.Forms.TextBox(); this.txt5 = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.label6 = new System.Windows.Forms.Label(); this.txt6 = new System.Windows.Forms.TextBox(); this.txt9 = new System.Windows.Forms.TextBox(); this.label7 = new System.Windows.Forms.Label(); this.label8 = new System.Windows.Forms.Label(); this.txt8 = new System.Windows.Forms.TextBox(); this.txt7 = new System.Windows.Forms.TextBox(); this.label9 = new System.Windows.Forms.Label(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginInit(); // // groupBox1 // this.groupBox1.Name = "groupBox1"; // // txt2 // this.txt2.Name = "txt2"; // // label1 // this.label1.Name = "label1"; this.label1.Text = "年级"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; // // btnSearch // this.btnSearch.Name = "btnSearch"; this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click); // // txt1 // this.txt1.Name = "txt1"; // // label2 // this.label2.Name = "label2"; this.label2.Text = "班级编号"; // // txt3 // this.txt3.Name = "txt3"; // // label3 // this.label3.Name = "label3"; this.label3.Text = "班级名称"; // // toolBar1 // this.toolBar1.Name = "toolBar1"; this.toolBar1.Size = new System.Drawing.Size(728, 41); // // dataGrid1 // this.dataGrid1.DataMember = "班级信息"; this.dataGrid1.DataSource = this.dataSet11; this.dataGrid1.Name = "dataGrid1"; // // groupBox2 // this.groupBox2.Controls.Add(this.txt4); this.groupBox2.Controls.Add(this.label4); this.groupBox2.Controls.Add(this.txt5); this.groupBox2.Controls.Add(this.label5); this.groupBox2.Controls.Add(this.label6); this.groupBox2.Controls.Add(this.txt6); this.groupBox2.Controls.Add(this.txt9); this.groupBox2.Controls.Add(this.label7); this.groupBox2.Controls.Add(this.label8); this.groupBox2.Controls.Add(this.txt8); this.groupBox2.Controls.Add(this.txt7); this.groupBox2.Controls.Add(this.label9); this.groupBox2.Name = "groupBox2"; // // da1 // this.da1.DeleteCommand = this.sqlDeleteCommand1; this.da1.InsertCommand = this.sqlInsertCommand1; this.da1.SelectCommand = this.sqlSelectCommand1; this.da1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "班级信息", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("班级编号", "班级编号"), new System.Data.Common.DataColumnMapping("年级", "年级"), new System.Data.Common.DataColumnMapping("班级名称", "班级名称"), new System.Data.Common.DataColumnMapping("班级简称", "班级简称"), new System.Data.Common.DataColumnMapping("人数", "人数"), new System.Data.Common.DataColumnMapping("班主任", "班主任")})}); this.da1.UpdateCommand = this.sqlUpdateCommand1; // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT 班级编号, 年级, 班级名称, 班级简称, 人数, 班主任 FROM 班级信息 WHERE (班级编号 LIKE @Param4) AND (年级 " + "LIKE @Param5) AND (班级名称 LIKE @Param6)"; this.sqlSelectCommand1.Connection = this.sqlConnection1; this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param4", System.Data.SqlDbType.VarChar, 14, "班级编号")); this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param5", System.Data.SqlDbType.VarChar, 4, "年级")); this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param6", System.Data.SqlDbType.VarChar, 30, "班级名称")); // // sqlConnection1 // this.sqlConnection1.ConnectionString = "server=.\\sqlexpress;database=eisbook;User id=sa;password=sa"; // // sqlInsertCommand1 // this.sqlInsertCommand1.CommandText = "INSERT INTO 班级信息(班级编号, 年级, 班级名称, 班级简称, 人数, 班主任) VALUES (@班级编号, @年级, @班级名称, @班级简称," + " @人数, @班主任); SELECT 班级编号, 年级, 班级名称, 班级简称, 人数, 班主任 FROM 班级信息 WHERE (班级编号 = @班级编号)" + ""; this.sqlInsertCommand1.Connection = this.sqlConnection1; this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级编号", System.Data.SqlDbType.VarChar, 14, "班级编号")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@年级", System.Data.SqlDbType.VarChar, 4, "年级")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级名称", System.Data.SqlDbType.VarChar, 30, "班级名称")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级简称", System.Data.SqlDbType.VarChar, 16, "班级简称")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@人数", System.Data.SqlDbType.Decimal, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(3)), ((System.Byte)(0)), "人数", System.Data.DataRowVersion.Current, null)); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班主任", System.Data.SqlDbType.VarChar, 8, "班主任")); // // sqlUpdateCommand1 // this.sqlUpdateCommand1.CommandText = @"UPDATE 班级信息 SET 班级编号 = @班级编号, 年级 = @年级, 班级名称 = @班级名称, 班级简称 = @班级简称, 人数 = @人数, 班主任 = @班主任 WHERE (班级编号 = @Original_班级编号) AND (人数 = @Original_人数 OR @Original_人数 IS NULL AND 人数 IS NULL) AND (年级 = @Original_年级 OR @Original_年级 IS NULL AND 年级 IS NULL) AND (班主任 = @Original_班主任 OR @Original_班主任 IS NULL AND 班主任 IS NULL) AND (班级名称 = @Original_班级名称 OR @Original_班级名称 IS NULL AND 班级名称 IS NULL) AND (班级简称 = @Original_班级简称 OR @Original_班级简称 IS NULL AND 班级简称 IS NULL); SELECT 班级编号, 年级, 班级名称, 班级简称, 人数, 班主任 FROM 班级信息 WHERE (班级编号 = @班级编号)"; this.sqlUpdateCommand1.Connection = this.sqlConnection1; this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级编号", System.Data.SqlDbType.VarChar, 14, "班级编号")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@年级", System.Data.SqlDbType.VarChar, 4, "年级")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级名称", System.Data.SqlDbType.VarChar, 30, "班级名称")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班级简称", System.Data.SqlDbType.VarChar, 16, "班级简称")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@人数", System.Data.SqlDbType.Decimal, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(3)), ((System.Byte)(0)), "人数", System.Data.DataRowVersion.Current, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@班主任", System.Data.SqlDbType.VarChar, 8, "班主任")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级编号", System.Data.SqlDbType.VarChar, 14, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级编号", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_人数", System.Data.SqlDbType.Decimal, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(3)), ((System.Byte)(0)), "人数", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_年级", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "年级", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班主任", System.Data.SqlDbType.VarChar, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班主任", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级名称", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级名称", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级简称", System.Data.SqlDbType.VarChar, 16, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级简称", System.Data.DataRowVersion.Original, null)); // // sqlDeleteCommand1 // this.sqlDeleteCommand1.CommandText = @"DELETE FROM 班级信息 WHERE (班级编号 = @Original_班级编号) AND (人数 = @Original_人数 OR @Original_人数 IS NULL AND 人数 IS NULL) AND (年级 = @Original_年级 OR @Original_年级 IS NULL AND 年级 IS NULL) AND (班主任 = @Original_班主任 OR @Original_班主任 IS NULL AND 班主任 IS NULL) AND (班级名称 = @Original_班级名称 OR @Original_班级名称 IS NULL AND 班级名称 IS NULL) AND (班级简称 = @Original_班级简称 OR @Original_班级简称 IS NULL AND 班级简称 IS NULL)"; this.sqlDeleteCommand1.Connection = this.sqlConnection1; this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级编号", System.Data.SqlDbType.VarChar, 14, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级编号", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_人数", System.Data.SqlDbType.Decimal, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(3)), ((System.Byte)(0)), "人数", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_年级", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "年级", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班主任", System.Data.SqlDbType.VarChar, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班主任", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级名称", System.Data.SqlDbType.VarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级名称", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_班级简称", System.Data.SqlDbType.VarChar, 16, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "班级简称", System.Data.DataRowVersion.Original, null)); // // dataSet11 // this.dataSet11.DataSetName = "DataSet1"; this.dataSet11.Locale = new System.Globalization.CultureInfo("zh-CN"); // // label4 // this.label4.Location = new System.Drawing.Point(64, 32); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(100, 16); this.label4.TabIndex = 0; this.label4.Text = "班级编号"; // // txt4 // this.txt4.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班级编号")); this.txt4.Location = new System.Drawing.Point(64, 48); this.txt4.Name = "txt4"; this.txt4.ReadOnly = true; this.txt4.Size = new System.Drawing.Size(136, 21); this.txt4.TabIndex = 1; this.txt4.Text = ""; // // txt5 // this.txt5.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.年级")); this.txt5.Location = new System.Drawing.Point(232, 48); this.txt5.Name = "txt5"; this.txt5.ReadOnly = true; this.txt5.Size = new System.Drawing.Size(136, 21); this.txt5.TabIndex = 1; this.txt5.Text = ""; // // label5 // this.label5.Location = new System.Drawing.Point(232, 32); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(100, 16); this.label5.TabIndex = 0; this.label5.Text = "年级"; // // label6 // this.label6.Location = new System.Drawing.Point(408, 32); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(100, 16); this.label6.TabIndex = 0; this.label6.Text = "班级名称"; // // txt6 // this.txt6.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班级名称")); this.txt6.Location = new System.Drawing.Point(408, 48); this.txt6.Name = "txt6"; this.txt6.ReadOnly = true; this.txt6.Size = new System.Drawing.Size(136, 21); this.txt6.TabIndex = 1; this.txt6.Text = ""; // // txt9 // this.txt9.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班主任")); this.txt9.Location = new System.Drawing.Point(408, 96); this.txt9.Name = "txt9"; this.txt9.ReadOnly = true; this.txt9.Size = new System.Drawing.Size(136, 21); this.txt9.TabIndex = 1; this.txt9.Text = ""; // // label7 // this.label7.Location = new System.Drawing.Point(408, 80); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(100, 16); this.label7.TabIndex = 0; this.label7.Text = "班主任"; // // label8 // this.label8.Location = new System.Drawing.Point(232, 80); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(100, 16); this.label8.TabIndex = 0; this.label8.Text = "人数"; // // txt8 // this.txt8.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班级简称")); this.txt8.Location = new System.Drawing.Point(232, 96); this.txt8.Name = "txt8"; this.txt8.ReadOnly = true; this.txt8.Size = new System.Drawing.Size(136, 21); this.txt8.TabIndex = 1; this.txt8.Text = ""; // // txt7 // this.txt7.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "班级信息.班级简称")); this.txt7.Location = new System.Drawing.Point(64, 96); this.txt7.Name = "txt7"; this.txt7.ReadOnly = true; this.txt7.Size = new System.Drawing.Size(136, 21); this.txt7.TabIndex = 1; this.txt7.Text = ""; // // label9 // this.label9.Location = new System.Drawing.Point(64, 80); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(100, 16); this.label9.TabIndex = 0; this.label9.Text = "班级简称"; // // ClassInfo // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(728, 502); this.Name = "ClassInfo"; this.Text = "【班级信息维护】"; this.Load += new System.EventHandler(this.ClassInfo_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.groupBox2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(); } #endregion //--------创建窗体时,读入数据------- private void ClassInfo_Load(object sender, System.EventArgs e) { da1.SelectCommand.Parameters[0].Value="%"; da1.SelectCommand.Parameters[1].Size=8;//4位年份输入 da1.SelectCommand.Parameters[1].Value="%"; da1.SelectCommand.Parameters[2].Value="%"; da1.Fill(dataSet11); //设置数据导航控件 this.cmOrders=(CurrencyManager) BindingContext[dataSet11,"班级信息"]; //将基类中的数据集与派生类中的数据集连接 base.dataSet11=this.dataSet11; } //-----------根据输入,检索信息---------- private void btnSearch_Click(object sender, System.EventArgs e) { da1.SelectCommand.Parameters[0].Value="%"; da1.SelectCommand.Parameters[1].Value="%"; da1.SelectCommand.Parameters[2].Value="%"; if(txt1.Text.Trim()!="") { da1.SelectCommand.Parameters[0].Value="%"+txt1.Text.Trim()+"%"; } if(txt2.Text.Trim()!="") { da1.SelectCommand.Parameters[1].Value="%"+txt2.Text.Trim()+"%"; } if(txt3.Text.Trim()!="") { da1.SelectCommand.Parameters[2].Value="%"+txt3.Text.Trim()+"%"; } dataSet11.Clear();//刷新数据集 da1.Fill(dataSet11); } //----------重写设置控件只读属性函数---------- protected override void SetModifyMode(bool blnEdit) { base.SetModifyMode (blnEdit); txt4.ReadOnly=!blnEdit; txt5.ReadOnly=!blnEdit; txt6.ReadOnly=!blnEdit; txt7.ReadOnly=!blnEdit; txt8.ReadOnly=!blnEdit; txt9.ReadOnly=!blnEdit; } //-------重写新增记录时设置默认值函数-------- protected override void SetDefaultValue() { base.SetDefaultValue (); } //-------重写检查非空字段函数-------- protected override bool CheckNotNull() { if(txt4.Text.Trim()=="")// 班级编号不能为空 { MessageBox.Show("班级编号不能为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop); return(false); } return base.CheckNotNull (); } } }
一个关于C#写的酒店管理系统。。 using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace 酒店管理系统 { public class ClientConsume : 医院管理系统.ParentForm { public static string RZid;//入住单编号 private System.Data.SqlClient.SqlConnection sqlConnection1; private 酒店管理系统.DataSet2 dataSet21; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox txt4; private System.Windows.Forms.TextBox txt5; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox txt6; private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label8; private System.Windows.Forms.ComboBox cmb1; private System.Windows.Forms.ComboBox cmb2; private System.Windows.Forms.Button btnSelect; private System.Windows.Forms.MonthCalendar calen1; 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.IContainer components = null; public ClientConsume() { // 该调用是 Windows 窗体设计器所必需的。 InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化 } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region 设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ClientConsume)); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.dataSet21 = new 酒店管理系统.DataSet2(); this.label4 = new System.Windows.Forms.Label(); this.txt4 = new System.Windows.Forms.TextBox(); this.txt5 = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.txt6 = new System.Windows.Forms.TextBox(); this.label6 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.label8 = new System.Windows.Forms.Label(); this.cmb1 = new System.Windows.Forms.ComboBox(); this.cmb2 = new System.Windows.Forms.ComboBox(); this.btnSelect = new System.Windows.Forms.Button(); this.calen1 = new System.Windows.Forms.MonthCalendar(); 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(); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit(); this.groupBox2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataSet21)).BeginInit(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Name = "groupBox1"; // // txt2 // this.txt2.Name = "txt2"; // // label1 // this.label1.Name = "label1"; this.label1.Text = "消费内容"; // // btnSearch // this.btnSearch.Name = "btnSearch"; this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click); // // txt1 // this.txt1.Name = "txt1"; // // label2 // this.label2.Name = "label2"; this.label2.Text = "入住单号"; // // txt3 // this.txt3.Name = "txt3"; this.txt3.Visible = false; // // label3 // this.label3.Name = "label3"; this.label3.Visible = false; // // toolBar1 // this.toolBar1.Name = "toolBar1"; this.toolBar1.Size = new System.Drawing.Size(728, 41); // // dataGrid1 // this.dataGrid1.DataMember = "帐单明细"; this.dataGrid1.DataSource = this.dataSet21; this.dataGrid1.Name = "dataGrid1"; this.dataGrid1.Size = new System.Drawing.Size(728, 240); // // groupBox2 // this.groupBox2.Controls.Add(this.btnSelect); this.groupBox2.Controls.Add(this.cmb1); this.groupBox2.Controls.Add(this.txt4); this.groupBox2.Controls.Add(this.label4); this.groupBox2.Controls.Add(this.txt5); this.groupBox2.Controls.Add(this.label5); this.groupBox2.Controls.Add(this.txt6); this.groupBox2.Controls.Add(this.label6); this.groupBox2.Controls.Add(this.label7); this.groupBox2.Controls.Add(this.label8); this.groupBox2.Controls.Add(this.cmb2); this.groupBox2.Location = new System.Drawing.Point(0, 336); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(728, 160); // // da1 // this.da1.DeleteCommand = this.sqlDeleteCommand1; this.da1.InsertCommand = this.sqlInsertCommand1; this.da1.SelectCommand = this.sqlSelectCommand1; this.da1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "帐单明细", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("帐单编号", "帐单编号"), new System.Data.Common.DataColumnMapping("入住单号", "入住单号"), new System.Data.Common.DataColumnMapping("消费内容", "消费内容"), new System.Data.Common.DataColumnMapping("消费金额", "消费金额"), new System.Data.Common.DataColumnMapping("消费时间", "消费时间"), new System.Data.Common.DataColumnMapping("备注", "备注")})}); this.da1.UpdateCommand = this.sqlUpdateCommand1; // // sqlConnection1 // this.sqlConnection1.ConnectionString = "Data Source=.\\sqlexpress;Initial Catalog=hotelbook;Integrated Security=True"; // // dataSet21 // this.dataSet21.DataSetName = "DataSet2"; this.dataSet21.Locale = new System.Globalization.CultureInfo("zh-CN"); // // label4 // this.label4.Location = new System.Drawing.Point(96, 16); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(100, 16); this.label4.TabIndex = 0; this.label4.Text = "入住单号"; // // txt4 // this.txt4.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet21, "帐单明细.入住单号")); this.txt4.Location = new System.Drawing.Point(96, 32); this.txt4.Name = "txt4"; this.txt4.ReadOnly = true; this.txt4.Size = new System.Drawing.Size(176, 21); this.txt4.TabIndex = 1; this.txt4.Text = ""; // // txt5 // this.txt5.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet21, "帐单明细.消费金额")); this.txt5.Location = new System.Drawing.Point(96, 80); this.txt5.Name = "txt5"; this.txt5.ReadOnly = true; this.txt5.Size = new System.Drawing.Size(176, 21); this.txt5.TabIndex = 1; this.txt5.Text = ""; // // label5 // this.label5.Location = new System.Drawing.Point(96, 64); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(100, 16); this.label5.TabIndex = 0; this.label5.Text = "消费金额"; // // txt6 // this.txt6.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet21, "帐单明细.备注")); this.txt6.Location = new System.Drawing.Point(96, 128); this.txt6.Name = "txt6"; this.txt6.ReadOnly = true; this.txt6.Size = new System.Drawing.Size(464, 21); this.txt6.TabIndex = 1; this.txt6.Text = ""; // // label6 // this.label6.Location = new System.Drawing.Point(96, 112); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(100, 16); this.label6.TabIndex = 0; this.label6.Text = "备注"; // // label7 // this.label7.Location = new System.Drawing.Point(352, 16); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(100, 16); this.label7.TabIndex = 0; this.label7.Text = "消费时间"; // // label8 // this.label8.Location = new System.Drawing.Point(352, 64); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(100, 16); this.label8.TabIndex = 0; this.label8.Text = "消费内容"; // // cmb1 // this.cmb1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet21, "帐单明细.消费时间")); this.cmb1.Enabled = false; this.cmb1.Location = new System.Drawing.Point(352, 32); this.cmb1.Name = "cmb1"; this.cmb1.Size = new System.Drawing.Size(208, 20); this.cmb1.TabIndex = 2; this.cmb1.DropDown += new System.EventHandler(this.cmb1_DropDown); this.cmb1.Leave += new System.EventHandler(this.cmb1_Leave); // // cmb2 // this.cmb2.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet21, "帐单明细.消费内容")); this.cmb2.Enabled = false; this.cmb2.Items.AddRange(new object[] { "加收房费", "加收水电费", "视频点播费", "宽带上网费", "商品使用费", "其他费用"}); this.cmb2.Location = new System.Drawing.Point(352, 80); this.cmb2.Name = "cmb2"; this.cmb2.Size = new System.Drawing.Size(208, 20); this.cmb2.TabIndex = 2; // // btnSelect // this.btnSelect.Enabled = false; this.btnSelect.Image = ((System.Drawing.Image)(resources.GetObject("btnSelect.Image"))); this.btnSelect.Location = new System.Drawing.Point(280, 32); this.btnSelect.Name = "btnSelect"; this.btnSelect.Size = new System.Drawing.Size(24, 24); this.btnSelect.TabIndex = 73; this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click); this.btnSelect.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btnSelect_MouseUp); // // calen1 // this.calen1.Location = new System.Drawing.Point(432, 208); this.calen1.Name = "calen1"; this.calen1.TabIndex = 131; this.calen1.Visible = false; this.calen1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.calen1_DateSelected); // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT 帐单编号, 入住单号, 消费内容, 消费金额, 消费时间, 备注 FROM 帐单明细 WHERE (入住单号 LIKE @Param3) AND (" + "消费内容 LIKE @Param4 OR 消费内容 IS NULL)"; this.sqlSelectCommand1.Connection = this.sqlConnection1; this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param3", System.Data.SqlDbType.VarChar, 36, "入住单号")); this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param4", System.Data.SqlDbType.VarChar, 40, "消费内容")); // // sqlInsertCommand1 // this.sqlInsertCommand1.CommandText = "INSERT INTO 帐单明细(入住单号, 消费内容, 消费金额, 消费时间, 备注) VALUES (@入住单号, @消费内容, @消费金额, @消费时间, " + "@备注); SELECT 帐单编号, 入住单号, 消费内容, 消费金额, 消费时间, 备注 FROM 帐单明细 WHERE (帐单编号 = @@IDENTITY" + ")"; this.sqlInsertCommand1.Connection = this.sqlConnection1; this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@入住单号", System.Data.SqlDbType.VarChar, 36, "入住单号")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@消费内容", System.Data.SqlDbType.VarChar, 40, "消费内容")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@消费金额", System.Data.SqlDbType.Money, 8, "消费金额")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@消费时间", System.Data.SqlDbType.DateTime, 4, "消费时间")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@备注", System.Data.SqlDbType.VarChar, 80, "备注")); // // sqlUpdateCommand1 // this.sqlUpdateCommand1.CommandText = @"UPDATE 帐单明细 SET 入住单号 = @入住单号, 消费内容 = @消费内容, 消费金额 = @消费金额, 消费时间 = @消费时间, 备注 = @备注 WHERE (帐单编号 = @Original_帐单编号) AND (入住单号 = @Original_入住单号) AND (备注 = @Original_备注 OR @Original_备注 IS NULL AND 备注 IS NULL) AND (消费内容 = @Original_消费内容 OR @Original_消费内容 IS NULL AND 消费内容 IS NULL) AND (消费时间 = @Original_消费时间 OR @Original_消费时间 IS NULL AND 消费时间 IS NULL) AND (消费金额 = @Original_消费金额 OR @Original_消费金额 IS NULL AND 消费金额 IS NULL); SELECT 帐单编号, 入住单号, 消费内容, 消费金额, 消费时间, 备注 FROM 帐单明细 WHERE (帐单编号 = @帐单编号)"; this.sqlUpdateCommand1.Connection = this.sqlConnection1; this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@入住单号", System.Data.SqlDbType.VarChar, 36, "入住单号")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@消费内容", System.Data.SqlDbType.VarChar, 40, "消费内容")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@消费金额", System.Data.SqlDbType.Money, 8, "消费金额")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@消费时间", System.Data.SqlDbType.DateTime, 4, "消费时间")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@备注", System.Data.SqlDbType.VarChar, 80, "备注")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_帐单编号", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "帐单编号", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_入住单号", System.Data.SqlDbType.VarChar, 36, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "入住单号", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_备注", System.Data.SqlDbType.VarChar, 80, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "备注", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_消费内容", System.Data.SqlDbType.VarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "消费内容", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_消费时间", System.Data.SqlDbType.DateTime, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "消费时间", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_消费金额", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "消费金额", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@帐单编号", System.Data.SqlDbType.Int, 4, "帐单编号")); // // sqlDeleteCommand1 // this.sqlDeleteCommand1.CommandText = @"DELETE FROM 帐单明细 WHERE (帐单编号 = @Original_帐单编号) AND (入住单号 = @Original_入住单号) AND (备注 = @Original_备注 OR @Original_备注 IS NULL AND 备注 IS NULL) AND (消费内容 = @Original_消费内容 OR @Original_消费内容 IS NULL AND 消费内容 IS NULL) AND (消费时间 = @Original_消费时间 OR @Original_消费时间 IS NULL AND 消费时间 IS NULL) AND (消费金额 = @Original_消费金额 OR @Original_消费金额 IS NULL AND 消费金额 IS NULL)"; this.sqlDeleteCommand1.Connection = this.sqlConnection1; this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_帐单编号", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "帐单编号", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_入住单号", System.Data.SqlDbType.VarChar, 36, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "入住单号", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_备注", System.Data.SqlDbType.VarChar, 80, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "备注", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_消费内容", System.Data.SqlDbType.VarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "消费内容", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_消费时间", System.Data.SqlDbType.DateTime, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "消费时间", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_消费金额", System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "消费金额", System.Data.DataRowVersion.Original, null)); // // ClientConsume // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(728, 502); this.Controls.Add(this.calen1); this.Name = "ClientConsume"; this.Text = "【消费记帐】"; this.Load += new System.EventHandler(this.ClientConsume_Load); this.Controls.SetChildIndex(this.groupBox1, 0); this.Controls.SetChildIndex(this.toolBar1, 0); this.Controls.SetChildIndex(this.dataGrid1, 0); this.Controls.SetChildIndex(this.groupBox2, 0); this.Controls.SetChildIndex(this.calen1, 0); ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit(); this.groupBox2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataSet21)).EndInit(); this.ResumeLayout(false); } #endregion //-----显示和消费日期----- private void cmb1_DropDown(object sender, System.EventArgs e) { selectDate(cmb1); } //----------关闭日期控件的显示---------- private void cmb1_Leave(object sender, System.EventArgs e) { calen1.Visible=false; } //-----------单击下拉列表框下拉箭头时显示日期以供选择--------------- private void selectDate(ComboBox cb) { calen1.Left=cb.Left;//设置日期控件的位置 calen1.Top=cb.Top-calen1.Height-10+groupBox2.Top; if(cb.Text.Trim()!="") { calen1.SelectionStart=Convert.ToDateTime(cb.Text);//日历显示的时间为数据时间 calen1.SelectionEnd=Convert.ToDateTime(cb.Text); } calen1.Visible=true;//显示日期 calen1.Show(); } //-----------从日历控件中选择日期信息------------ private void calen1_DateSelected(object sender, System.Windows.Forms.DateRangeEventArgs e) { cmb1.Text=calen1.SelectionEnd.ToString(); } //-------------根据输入选择消费数据--------------- private void btnSearch_Click(object sender, System.EventArgs e) { da1.SelectCommand.Parameters[0].Value="%"; da1.SelectCommand.Parameters[1].Value="%"; if(txt1.Text.Trim()!="") { da1.SelectCommand.Parameters[0].Value="%"+txt1.Text.Trim()+"%"; } if(txt2.Text.Trim()!="") { da1.SelectCommand.Parameters[1].Value="%"+txt2.Text.Trim()+"%"; } dataSet21.Clear();//刷新数据集 da1.Fill(dataSet21); } private void ClientConsume_Load(object sender, System.EventArgs e) { da1.SelectCommand.Parameters[0].Value="%"; da1.SelectCommand.Parameters[1].Value="%"; da1.Fill(dataSet21);//填充帐单明细 base.dataSet11=this.dataSet21; this.cmOrders=(CurrencyManager) BindingContext[dataSet21,"帐单明细"]; } //----------重写设置控件只读属性函数---------- protected override void SetModifyMode(bool blnEdit) { base.SetModifyMode (blnEdit); txt5.ReadOnly=!blnEdit; txt6.ReadOnly=!blnEdit; cmb1.Enabled=blnEdit; cmb2.Enabled=blnEdit; btnSelect.Enabled=blnEdit; } //-------重写新增记录时设置默认值函数-------- protected override void SetDefaultValue() { base.SetDefaultValue(); } //-------重写检查非空字段函数-------- protected override bool CheckNotNull() { if(txt4.Text.Trim()=="")// 检查入住单号 { MessageBox.Show("入住单号号不能为空","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop); return(false); } if(txt5.Text.Trim()=="")//检查消费金额 { MessageBox.Show("请输入消费金额","提示",MessageBoxButtons.OK,MessageBoxIcon.Stop); return(false); } return base.CheckNotNull (); } //----------从入住单中双击选择入住单号----------- private void btnSelect_Click(object sender, System.EventArgs e) { string sql="select * from 入住单"; SelectTable newfrm=new SelectTable(sql,2); newfrm.Text="双击选择入住单号"; newfrm.ShowDialog(); } private void btnSelect_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(RZid=="") return; txt4.Text=RZid; } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace 保存GRID数据示例 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { //提示是否修改 #region//--------修改数据就将数据保存并显示 if (MessageBox.Show("是否保存数据?", "系统消息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK) { #region ..........这里是保存数据代码 //结束编辑 dataGridView1.EndEdit(); //重新用表格数据填充数据容器 OleDbDataAdapter Ada = new OleDbDataAdapter(); DataTable table = (DataTable)dataGridView1.DataSource; //重新启动连接 String ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "/驱动.mdb"; //用Buider方法更新数据 using (OleDbConnection connection = new OleDbConnection(ConnectionString)) { Ada.SelectCommand = new OleDbCommand("SELECT * FROM 表", connection); OleDbCommandBuilder builder = new OleDbCommandBuilder(Ada); Ada.UpdateCommand = builder.GetUpdateCommand(); try { //更新数据表数据时 Ada.Update(table); table.AcceptChanges(); MessageBox.Show("操作已成功!数据将全部被保存......", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2); } catch (System.Data.OleDb.OleDbException ex) { throw new Exception(ex.Message); } } #endregion } #endregion #region //--------不修改就初始化显示以前数据 else { MessageBox.Show("用户取消操作,数据将恢复到初始状态......"); OleDbConnection A = new OleDbConnection(); A.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "/驱动.mdb"; try { A.Open(); DataSet B = new DataSet(); string sqlStr = "Select * from 表"; OleDbDataAdapter C = new OleDbDataAdapter(sqlStr, A); C.Fill(B); dataGridView1.DataSource = B.Tables[0]; } catch (System.Data.OleDb.OleDbException ex) { throw new Exception(ex.Message); } finally { A.Close(); } } #endregion } private void button1_Click(object sender, EventArgs e) { OleDbConnection A = new OleDbConnection(); A.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "/驱动.mdb"; try { A.Open(); DataSet B = new DataSet(); string sqlStr = "Select * from 表"; OleDbDataAdapter C = new OleDbDataAdapter(sqlStr, A); C.Fill(B); dataGridView1.DataSource = B.Tables[0]; } catch (System.Data.OleDb.OleDbException ex) { throw new Exception(ex.Message); } finally { A.Close(); } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } } }

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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