📄 bookclassify.cs
字号:
this.txt4.Name = "txt4";
this.txt4.ReadOnly = true;
this.txt4.TabIndex = 2;
this.txt4.Text = "";
//
// label4
//
this.label4.Location = new System.Drawing.Point(392, 200);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 16);
this.label4.TabIndex = 1;
this.label4.Text = "可借天数";
//
// txt5
//
this.txt5.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.dataSet11, "图书类型.类型名称"));
this.txt5.Location = new System.Drawing.Point(392, 280);
this.txt5.Name = "txt5";
this.txt5.ReadOnly = true;
this.txt5.Size = new System.Drawing.Size(168, 21);
this.txt5.TabIndex = 2;
this.txt5.Text = "";
//
// label5
//
this.label5.Location = new System.Drawing.Point(392, 264);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(64, 16);
this.label5.TabIndex = 1;
this.label5.Text = "类型名称";
//
// 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("可借天数", "可借天数")})});
this.da1.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = "DELETE FROM 图书类型 WHERE (类型编号 = @Original_类型编号) AND (可借天数 = @Original_可借天数 OR @Ori" +
"ginal_可借天数 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, 50, 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.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, 100, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "类型名称", System.Data.DataRowVersion.Original, null));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=localhost;Integrated Security=SSPI;Database=libbook;";
//
// 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, 50, "类型编号"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@类型名称", System.Data.SqlDbType.VarChar, 100, "类型名称"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@可借天数", System.Data.SqlDbType.Int, 4, "可借天数"));
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT 类型编号, 类型名称, 可借天数 FROM 图书类型 WHERE (类型编号 LIKE @Param3) AND (类型名称 LIKE @Param" +
"4)";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param3", System.Data.SqlDbType.VarChar, 50, "类型编号"));
this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param4", System.Data.SqlDbType.VarChar, 100, "类型名称"));
//
// 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); SELECT 类型编号, 类型名称, 可借天数 FROM 图书类型 WHERE (类型编号 = @类型编号)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@类型编号", System.Data.SqlDbType.VarChar, 50, "类型编号"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@类型名称", System.Data.SqlDbType.VarChar, 100, "类型名称"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@可借天数", System.Data.SqlDbType.Int, 4, "可借天数"));
this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_类型编号", System.Data.SqlDbType.VarChar, 50, 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.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, 100, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "类型名称", System.Data.DataRowVersion.Original, null));
//
// BookClassify
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(584, 485);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.txt3);
this.Controls.Add(this.label3);
this.Controls.Add(this.txt4);
this.Controls.Add(this.label4);
this.Controls.Add(this.txt5);
this.Controls.Add(this.label5);
this.Name = "BookClassify";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "【图书类型设置】";
this.Load += new System.EventHandler(this.BookClassify_Load);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
//---------显示窗口,读入全部数据-----------
private void BookClassify_Load(object sender, System.EventArgs e)
{
da1.SelectCommand.Parameters[0].Value="%%";
da1.SelectCommand.Parameters[1].Value="%%";
da1.Fill(dataSet11);
//设置数据导航
cmOrders=(CurrencyManager) BindingContext[dataSet11,"图书类型"];
}
//----------根据输入的信息,查询图书类型信息--------
private void btnSearch_Click(object sender, System.EventArgs e)
{
da1.SelectCommand.Parameters[0].Value="%%";
da1.SelectCommand.Parameters[1].Value="%%";
if(txt1.Text!="")
{
da1.SelectCommand.Parameters[0].Value="%"+txt1.Text+"%";
}
if(txt2.Text!="")
{
da1.SelectCommand.Parameters[1].Value="%"+txt2.Text+"%";
}
dataSet11.图书类型.Clear();
da1.Fill(dataSet11);
}
//-------------处理工具栏事务------------
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button.ToolTipText == "首记录")
{
this.dataGrid1.UnSelect(cmOrders.Position); //取消原选中的行
cmOrders.Position = 0;
this.dataGrid1.Select(cmOrders.Position); //选中当前行
this.dataGrid1.CurrentRowIndex = cmOrders.Position; //移动表头指示图标
return;
}
if (e.Button.ToolTipText == "上一记录")
{
if (cmOrders.Position >= 0)
{
this.dataGrid1.UnSelect(cmOrders.Position);
cmOrders.Position--;
this.dataGrid1.Select(cmOrders.Position);
this.dataGrid1.CurrentRowIndex = cmOrders.Position;
}
return;
}
if (e.Button.ToolTipText == "下一记录")
{
if (cmOrders.Position <= cmOrders.Count-1)
{
this.dataGrid1.UnSelect(cmOrders.Position);
cmOrders.Position++;
this.dataGrid1.Select(cmOrders.Position);
this.dataGrid1.CurrentRowIndex = cmOrders.Position;
}
return;
}
if (e.Button.ToolTipText == "尾记录")
{
this.dataGrid1.UnSelect(cmOrders.Position);
cmOrders.Position = cmOrders.Count-1;
this.dataGrid1.Select(cmOrders.Position);
this.dataGrid1.CurrentRowIndex = cmOrders.Position;
return;
}
if(e.Button.ToolTipText=="新增")
{
cmOrders.AddNew();
//设置默认值
SetDefaultValue();
//设置编辑属性
SetModifyMode(true);
}
if(e.Button.ToolTipText=="修改")
{
SetModifyMode(true);
}
if(e.Button.ToolTipText=="删除")
{
DialogResult result=MessageBox.Show("确认删除?","删除数据",MessageBoxButtons.OKCancel);
if(result==DialogResult.OK)
if(cmOrders.Count>0)//立即从数据集中删除
{
cmOrders.RemoveAt(cmOrders.Position);
da1.Update(dataSet11);
}
else
MessageBox.Show("表中为空,已无可删除数据","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
if(e.Button.ToolTipText=="提交")
{
if(txt3.Text.Trim()=="")//检查字段
{
MessageBox.Show("请输入类型编号","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
if(txt5.Text.Trim()=="")
{
MessageBox.Show("请输入类型名称","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
return;
}
cmOrders.EndCurrentEdit();
if(dataSet11.GetChanges()!=null)
{
try
{
da1.Update(dataSet11);
SetModifyMode(false);
}
catch(Exception express)
{
MessageBox.Show(express.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
dataSet11.RejectChanges();
}
}
return;
}
if (e.Button.ToolTipText == "取消")
{
try
{
cmOrders.CancelCurrentEdit(); //取消编辑
SetModifyMode(false);
}
catch(Exception express)
{
MessageBox.Show(express.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
return;
}
if(e.Button.ToolTipText=="退出")
{
this.Close();
}
}
//--------------对控件的属性做设置---------------
private void SetModifyMode(bool blnEdit)
{
//设置文本框和下拉列表框属性
txt4.ReadOnly=!blnEdit;
txt5.ReadOnly=!blnEdit;
txt3.ReadOnly=!blnEdit;
//设置搜索按钮属性
btnSearch.Enabled=!blnEdit;
}
//------------在新增数据时,设置默认值--------------
private void SetDefaultValue()
{
string strConn = "workstation id=localhost;Integrated Security=SSPI;database=libbook";
SqlConnection cn=new SqlConnection(strConn);
cn.Open();
SqlCommand cmd=cn.CreateCommand();
cmd.CommandText="select max(类型编号) 最大编号 from 图书类型";
int maxID=1;//计算新编号
object result=cmd.ExecuteScalar();
if(result!=System.DBNull.Value)
{
maxID=Convert.ToInt32(result)+1;
}
int length=maxID.ToString().Length;
switch(length)
{
case 1:
txt3.Text="00"+maxID.ToString();
break;
case 2:
txt3.Text="0"+maxID.ToString();
break;
case 3:
txt3.Text=maxID.ToString();
break;
}
txt4.Text="1";//设置默认可借天数
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -