简单的插入数据并作一定判断

HTML代码
None.gif <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > lanchudengji </ title >
None.gif        
< meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
None.gif        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
None.gif        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
None.gif        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
ExpandedBlockStart.gifContractedBlock.gif        
< style  type ="text/css" > dot.gif BODY {dot.gif}{ BACKGROUND-COLOR: #def3ff }
ExpandedBlockEnd.gif        
</ style >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< FONT  face ="宋体" >
None.gif                
< asp:label  id ="Label1"  style ="Z-INDEX: 101; LEFT: 168px; POSITION: absolute; TOP: 56px"  runat ="server"
None.gif                    Font-Size
="Smaller"  ForeColor ="#004040" > 职工姓名: </ asp:label >< asp:label  id ="Label2"  style ="Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 88px"  runat ="server"
None.gif                    Font-Size
="Smaller"  ForeColor ="#004040" > 所属部门: </ asp:label >< asp:dropdownlist  id ="DropDownList1"  style ="Z-INDEX: 103; LEFT: 248px; POSITION: absolute; TOP: 88px"
None.gif                    tabIndex
="2"  runat ="server" ></ asp:dropdownlist >< asp:label  id ="Label3"  style ="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 120px"  runat ="server"
None.gif                    Font-Size
="Smaller"  ForeColor ="#004040" > 揽储帐号: </ asp:label >< asp:textbox  id ="TextBox2"  style ="Z-INDEX: 105; LEFT: 248px; POSITION: absolute; TOP: 120px"
None.gif                    tabIndex
="3"  runat ="server"  BorderStyle ="Groove"  ToolTip ="活期帐号应该为19位" ></ asp:textbox >< asp:label  id ="Label4"  style ="Z-INDEX: 106; LEFT: 168px; POSITION: absolute; TOP: 152px"  runat ="server"
None.gif                    Font-Size
="Smaller"  ForeColor ="#004040" > 揽储金额: </ asp:label >< asp:textbox  id ="TextBox3"  style ="Z-INDEX: 107; LEFT: 248px; POSITION: absolute; TOP: 152px"
None.gif                    tabIndex
="4"  runat ="server"  BorderStyle ="Groove"  ToolTip ="金额为非零开头的正数" ></ asp:textbox >< asp:label  id ="Label5"  style ="Z-INDEX: 108; LEFT: 168px; POSITION: absolute; TOP: 184px"  runat ="server"
None.gif                    Font-Size
="Smaller"  ForeColor ="#004040" > 揽储类型: </ asp:label >< asp:dropdownlist  id ="DropDownList2"  style ="Z-INDEX: 109; LEFT: 248px; POSITION: absolute; TOP: 184px"
None.gif                    tabIndex
="5"  runat ="server" >
None.gif                    
< asp:ListItem  Value ="活期" > 活期 </ asp:ListItem >
None.gif                
</ asp:dropdownlist >< asp:label  id ="Label6"  style ="Z-INDEX: 110; LEFT: 168px; POSITION: absolute; TOP: 216px"  runat ="server"
None.gif                    Font-Size
="Smaller"  ForeColor ="#004040" > 客户类型: </ asp:label >< asp:dropdownlist  id ="DropDownList3"  style ="Z-INDEX: 111; LEFT: 248px; POSITION: absolute; TOP: 216px"
None.gif                    tabIndex
="6"  runat ="server"  BorderStyle ="Groove"  Height ="16px" ></ asp:dropdownlist >< asp:label  id ="Label7"  style ="Z-INDEX: 112; LEFT: 168px; POSITION: absolute; TOP: 248px"  runat ="server"
None.gif                    Font-Size
="Smaller"  ForeColor ="#004040"  Height ="8px" > 存入日期: </ asp:label >< asp:textbox  id ="TextBox4"  style ="Z-INDEX: 113; LEFT: 248px; POSITION: absolute; TOP: 248px"
None.gif                    tabIndex
="7"  runat ="server"  BorderStyle ="Groove"  ToolTip ="格式:050101" ></ asp:textbox >< asp:button  id ="Button1"  style ="Z-INDEX: 114; LEFT: 248px; POSITION: absolute; TOP: 288px"  tabIndex ="8"
None.gif                    runat
="server"  BorderStyle ="Groove"  Text ="确 认" ></ asp:button >< asp:requiredfieldvalidator  id ="RequiredFieldValidator1"  style ="Z-INDEX: 115; LEFT: 416px; POSITION: absolute; TOP: 120px"
None.gif                    runat
="server"  Font-Size ="Smaller"  ErrorMessage ="请输入揽储帐号"  ControlToValidate ="TextBox2" ></ asp:requiredfieldvalidator >< asp:requiredfieldvalidator  id ="RequiredFieldValidator2"  style ="Z-INDEX: 116; LEFT: 416px; POSITION: absolute; TOP: 152px"
None.gif                    runat
="server"  Font-Size ="Smaller"  ErrorMessage ="请输入揽储金额"  ControlToValidate ="TextBox3" ></ asp:requiredfieldvalidator >< asp:requiredfieldvalidator  id ="RequiredFieldValidator3"  style ="Z-INDEX: 117; LEFT: 416px; POSITION: absolute; TOP: 248px"
None.gif                    runat
="server"  Font-Size ="Smaller"  ErrorMessage ="请输入日期"  ControlToValidate ="TextBox4" ></ asp:requiredfieldvalidator >< asp:regularexpressionvalidator  id ="RegularExpressionValidator1"  style ="Z-INDEX: 118; LEFT: 416px; POSITION: absolute; TOP: 128px"
None.gif                    runat
="server"  Font-Size ="Smaller"  ToolTip ="活期帐号应为18位数字"  ErrorMessage ="活期帐号应为18位数字"  ControlToValidate ="TextBox2"  ValidationExpression ="^\d{18}$" ></ asp:regularexpressionvalidator >< asp:regularexpressionvalidator  id ="RegularExpressionValidator3"  style ="Z-INDEX: 119; LEFT: 416px; POSITION: absolute; TOP: 160px"
None.gif                    runat
="server"  Font-Size ="Smaller"  ErrorMessage ="只能输入正数,可以带2位小数"  ControlToValidate ="TextBox3"  ValidationExpression ="^[0-9]+(.[0-9]{2})?$" ></ asp:regularexpressionvalidator >< asp:regularexpressionvalidator  id ="RegularExpressionValidator2"  style ="Z-INDEX: 120; LEFT: 416px; POSITION: absolute; TOP: 248px"
None.gif                    runat
="server"  Font-Size ="Smaller"  ErrorMessage ="时间格式应该为“年月日”(如050101),并且月<=12,日<=31"  ControlToValidate ="TextBox4"  ValidationExpression ="^(\d{2})((0?[1-9])|(1[0-2]))((0?[1-9])|((1|2)[0-9])|30|31)$" ></ asp:regularexpressionvalidator >
None.gif                
< asp:RegularExpressionValidator  id ="RegularExpressionValidator4"  style ="Z-INDEX: 121; LEFT: 416px; POSITION: absolute; TOP: 248px"
None.gif                    runat
="server"  Font-Size ="Smaller"  ErrorMessage ="时间格式应该为“年月日”(如050101),并且月<=12,日<=31"  ControlToValidate ="TextBox4"
None.gif                    ValidationExpression
="\d{6}$" ></ asp:RegularExpressionValidator >
None.gif                
< asp:TextBox  id ="TextBox5"  style ="Z-INDEX: 122; LEFT: 248px; POSITION: absolute; TOP: 24px"  runat ="server"
None.gif                    BorderStyle
="Groove"  AutoPostBack ="True" ></ asp:TextBox >
None.gif                
< asp:DropDownList  id ="DropDownList4"  style ="Z-INDEX: 123; LEFT: 248px; POSITION: absolute; TOP: 56px"
None.gif                    runat
="server"  tabIndex ="1" ></ asp:DropDownList >
None.gif                
< asp:Label  id ="Label8"  style ="Z-INDEX: 124; LEFT: 168px; POSITION: absolute; TOP: 24px"  runat ="server"
None.gif                    ForeColor
="#004040"  Font-Size ="Smaller" > 职工编号: </ asp:Label >
None.gif                
< asp:Button  id ="Button2"  style ="Z-INDEX: 125; LEFT: 416px; POSITION: absolute; TOP: 24px"  runat ="server"
None.gif                    Text
="Button" ></ asp:Button ></ FONT ></ form >
ExpandedBlockStart.gifContractedBlock.gif        
< script  language ="javascript"  for ="document"  event ="onkeydown" > dot.gif  
InBlock.gif
InBlock.gif
<!-- 
InBlock.gif
InBlock.gif  
if(event.keyCode==13 && event.srcElement.type!='button' && event.srcElement.type!='submit' && event.srcElement.type!='reset' && event.srcElement.type!='') 
InBlock.gif
InBlock.gif     event.keyCode
=9
InBlock.gif
InBlock.gif
--> 
InBlock.gif
ExpandedBlockEnd.gif        
</ script >
None.gif    
</ body >
None.gif
</ HTML >
None.gif

后台代码

None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
namespace  lanchuxitong
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// lanchudengji 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class huoqiluru : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label2;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList1;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label3;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label4;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox3;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label5;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList2;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label6;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList3;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox4;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif        
protected System.Data.SqlClient.SqlConnection sqlConnection1;
InBlock.gif        
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
InBlock.gif        
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
InBlock.gif        
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList4;
InBlock.gif        
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter2;
InBlock.gif        
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
InBlock.gif        
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
InBlock.gif        
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter3;
InBlock.gif        
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
InBlock.gif        
protected System.Data.SqlClient.SqlCommand sqlSelectCommand3;
InBlock.gif        
protected System.Data.SqlClient.SqlCommand sqlInsertCommand3;
InBlock.gif        
protected System.Data.SqlClient.SqlConnection sqlConnection2;
InBlock.gif        
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox2;
InBlock.gif        
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator3;
InBlock.gif        
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator2;
InBlock.gif        
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator4;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox5;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label8;
InBlock.gif        
protected System.Data.SqlClient.SqlCommand sqlSelectCommand2;
InBlock.gif        
protected System.Data.SqlClient.SqlCommand sqlInsertCommand2;
InBlock.gif        
protected System.Data.SqlClient.SqlConnection sqlConnection3;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button2;
InBlock.gif        
protected System.Web.UI.WebControls.Label Label7;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(HttpContext.Current.User.IsInRole("registrar")==false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{                
InBlock.gif                Response.ClearContent();
InBlock.gif                Response.ClearHeaders();
InBlock.gif                Response.Write(
"您没有权限执行该操作!");                
InBlock.gif                Response.Flush();
InBlock.gif                Response.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//*if(!this.IsPostBack)
InBlock.gif            {
InBlock.gif                TextBox5.Text=DateTime.Now.ToString("yyyy-MM-dd");
ExpandedSubBlockEnd.gif            }
*/

InBlock.gif            
if(!this.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                zjmDataBind();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
InBlock.gif            
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
InBlock.gif            
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.sqlDataAdapter2 = new System.Data.SqlClient.SqlDataAdapter();
InBlock.gif            
this.sqlDataAdapter3 = new System.Data.SqlClient.SqlDataAdapter();
InBlock.gif            
this.sqlInsertCommand3 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.sqlConnection2 = new System.Data.SqlClient.SqlConnection();
InBlock.gif            
this.sqlSelectCommand3 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.sqlInsertCommand2 = new System.Data.SqlClient.SqlCommand();
InBlock.gif            
this.sqlConnection3 = new System.Data.SqlClient.SqlConnection();
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
// 
InBlock.gif            
// sqlConnection1
InBlock.gif            
// 
InBlock.gif
            this.sqlConnection1.ConnectionString = "user id=" + System.Configuration.ConfigurationSettings.AppSettings["UserID"+ ";data source=" +  System.Configuration.ConfigurationSettings.AppSettings["DbServerName"]
InBlock.gif                
+ ";persist security info=True;initial catalog=" + System.Configuration.ConfigurationSettings.AppSettings["DbName"+ ";password=" + System.Configuration.ConfigurationSettings.AppSettings["Password"];
InBlock.gif            
this.sqlConnection1.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage);
InBlock.gif            
// 
InBlock.gif            
// sqlDataAdapter1
InBlock.gif            
// 
InBlock.gif
            this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
InBlock.gif            
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                                                                      
new System.Data.Common.DataTableMapping("Table""bumenxinxi"new System.Data.Common.DataColumnMapping[] dot.gif{
InBlock.gif                                                                                                                                                                                                                    
new System.Data.Common.DataColumnMapping("bumenbianma""bumenbianma"),
ExpandedSubBlockEnd.gif                                                                                                                                                                                                                    
new System.Data.Common.DataColumnMapping("bumenmingcheng""bumenmingcheng")}
)}
);
InBlock.gif            
// 
InBlock.gif            
// sqlInsertCommand1
InBlock.gif            
// 
InBlock.gif
            this.sqlInsertCommand1.CommandText = "INSERT INTO bumenxinxi(bumenbianma, bumenmingcheng) VALUES (@bumenbianma, @bumenm" +
InBlock.gif                
"ingcheng); SELECT bumenbianma, bumenmingcheng FROM bumenxinxi";
InBlock.gif            
this.sqlInsertCommand1.Connection = this.sqlConnection1;
InBlock.gif            
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@bumenbianma", System.Data.SqlDbType.VarChar, 10"bumenbianma"));
InBlock.gif            
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@bumenmingcheng", System.Data.SqlDbType.NVarChar, 50"bumenmingcheng"));
InBlock.gif            
// 
InBlock.gif            
// sqlSelectCommand1
InBlock.gif            
// 
InBlock.gif
            this.sqlSelectCommand1.CommandText = "SELECT bumenbianma, bumenmingcheng FROM bumenxinxi";
InBlock.gif            
this.sqlSelectCommand1.Connection = this.sqlConnection1;
InBlock.gif            
// 
InBlock.gif            
// sqlDataAdapter2
InBlock.gif            
// 
InBlock.gif
            this.sqlDataAdapter2.InsertCommand = this.sqlInsertCommand2;
InBlock.gif            
this.sqlDataAdapter2.SelectCommand = this.sqlSelectCommand2;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.sqlDataAdapter2.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                                                                      
new System.Data.Common.DataTableMapping("Table""zhigongxinxi"new System.Data.Common.DataColumnMapping[] dot.gif{
InBlock.gif                                                                                                                                                                                                                      
new System.Data.Common.DataColumnMapping("zhigongbianma""zhigongbianma"),
ExpandedSubBlockEnd.gif                                                                                                                                                                                                                      
new System.Data.Common.DataColumnMapping("zhigongxingming""zhigongxingming")}
)}
);
InBlock.gif            
// 
InBlock.gif            
// sqlDataAdapter3
InBlock.gif            
// 
InBlock.gif
            this.sqlDataAdapter3.InsertCommand = this.sqlInsertCommand3;
InBlock.gif            
this.sqlDataAdapter3.SelectCommand = this.sqlSelectCommand3;
ExpandedSubBlockStart.gifContractedSubBlock.gif            
this.sqlDataAdapter3.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                                                                                                      
new System.Data.Common.DataTableMapping("Table""kehuleixing"new System.Data.Common.DataColumnMapping[] dot.gif{
InBlock.gif                                                                                                                                                                                                                     
new System.Data.Common.DataColumnMapping("bianma""bianma"),
ExpandedSubBlockEnd.gif                                                                                                                                                                                                                     
new System.Data.Common.DataColumnMapping("kehuleixing""kehuleixing")}
)}
);
InBlock.gif            
// 
InBlock.gif            
// sqlInsertCommand3
InBlock.gif            
// 
InBlock.gif
            this.sqlInsertCommand3.CommandText = "INSERT INTO kehuleixing(bianma, kehuleixing) VALUES (@bianma, @kehuleixing); SELE" +
InBlock.gif                
"CT bianma, kehuleixing FROM kehuleixing";
InBlock.gif            
this.sqlInsertCommand3.Connection = this.sqlConnection2;
InBlock.gif            
this.sqlInsertCommand3.Parameters.Add(new System.Data.SqlClient.SqlParameter("@bianma", System.Data.SqlDbType.VarChar, 10"bianma"));
InBlock.gif            
this.sqlInsertCommand3.Parameters.Add(new System.Data.SqlClient.SqlParameter("@kehuleixing", System.Data.SqlDbType.NVarChar, 50"kehuleixing"));
InBlock.gif            
// 
InBlock.gif            
// sqlConnection2
InBlock.gif            
// 
InBlock.gif
            this.sqlConnection2.ConnectionString = "user id=" + System.Configuration.ConfigurationSettings.AppSettings["UserID"+ ";data source=" +  System.Configuration.ConfigurationSettings.AppSettings["DbServerName"]
InBlock.gif                
+ ";persist security info=True;initial catalog=" + System.Configuration.ConfigurationSettings.AppSettings["DbName"+ ";password=" + System.Configuration.ConfigurationSettings.AppSettings["Password"];
InBlock.gif            
// 
InBlock.gif            
// sqlSelectCommand3
InBlock.gif            
// 
InBlock.gif
            this.sqlSelectCommand3.CommandText = "SELECT bianma, kehuleixing FROM kehuleixing";
InBlock.gif            
this.sqlSelectCommand3.Connection = this.sqlConnection2;
InBlock.gif            
// 
InBlock.gif            
// sqlSelectCommand2
InBlock.gif            
// 
InBlock.gif
            this.sqlSelectCommand2.CommandText = "SELECT zhigongbianma, zhigongxingming FROM zhigongxinxi ORDER BY zhigongxingming";
InBlock.gif            
this.sqlSelectCommand2.Connection = this.sqlConnection3;
InBlock.gif            
// 
InBlock.gif            
// sqlInsertCommand2
InBlock.gif            
// 
InBlock.gif
            this.sqlInsertCommand2.CommandText = "INSERT INTO zhigongxinxi(zhigongxingming) VALUES (@zhigongxingming); SELECT zhigo" +
InBlock.gif                
"ngbianma, zhigongxingming FROM zhigongxinxi ORDER BY zhigongxingming";
InBlock.gif            
this.sqlInsertCommand2.Connection = this.sqlConnection3;
InBlock.gif            
this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@zhigongxingming", System.Data.SqlDbType.NVarChar, 20"zhigongxingming"));
InBlock.gif            
// 
InBlock.gif            
// sqlConnection3
InBlock.gif            
// 
InBlock.gif
            this.sqlConnection3.ConnectionString = "user id=" + System.Configuration.ConfigurationSettings.AppSettings["UserID"+ ";data source=" +  System.Configuration.ConfigurationSettings.AppSettings["DbServerName"]
InBlock.gif                
+ ";persist security info=True;initial catalog=" + System.Configuration.ConfigurationSettings.AppSettings["DbName"+ ";password=" + System.Configuration.ConfigurationSettings.AppSettings["Password"];
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void zjmDataBind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataSet dsLanChu 
= new DataSet();            
InBlock.gif
InBlock.gif            sqlDataAdapter1.Fill(dsLanChu, 
"BuMen");
InBlock.gif            
InBlock.gif            DropDownList1.DataSource 
= dsLanChu.Tables["BuMen"]; 
InBlock.gif            DropDownList1.DataTextField 
= "bumenmingcheng";
InBlock.gif            DropDownList1.DataValueField 
= "bumenbianma";
InBlock.gif            DropDownList1.DataBind();
InBlock.gif
InBlock.gif            sqlDataAdapter2.Fill(dsLanChu, 
"ZhiGong");
InBlock.gif            
InBlock.gif            DropDownList4.DataSource 
= dsLanChu.Tables["ZhiGong"]; 
InBlock.gif            DropDownList4.DataTextField 
= "zhigongxingming";
InBlock.gif            DropDownList4.DataValueField 
= "zhigongbianma";
InBlock.gif            DropDownList4.DataBind();
InBlock.gif
InBlock.gif            sqlDataAdapter3.Fill(dsLanChu, 
"KeHu");
InBlock.gif            
InBlock.gif            DropDownList3.DataSource 
= dsLanChu.Tables["KeHu"]; 
InBlock.gif            DropDownList3.DataTextField 
= "kehuleixing";
InBlock.gif            DropDownList3.DataValueField 
= "bianma";
InBlock.gif            DropDownList3.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Button1_Click(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sqlConnection1.Open();
InBlock.gif            SqlCommand sqlcmd1 
= new SqlCommand("select * from lanchudengji where chuhuzhanghao='"+ TextBox2.Text +"'", sqlConnection1);
InBlock.gif            SqlDataReader sqlreader1 
= sqlcmd1.ExecuteReader();
InBlock.gif
InBlock.gif            
if(sqlreader1.HasRows == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sqlreader1.Close();
ExpandedSubBlockStart.gifContractedSubBlock.gif                
/**//*sqlreader2.Close();*/
InBlock.gif                SqlCommand cmd1 
= new SqlCommand("insert into lanchudengji (zhigongming,suoshubumen,chuhuzhanghao,lanchujine,lanchuleixing,dakehu,cunruriqi,lururiqi) values ('"+ DropDownList4.SelectedItem.Text +"','"+ DropDownList1.SelectedItem.Value +"','" + TextBox2.Text + "','"+ TextBox3.Text +"','"+ DropDownList2.SelectedItem.Text +"','"+ DropDownList3.SelectedItem.Text +"','"+ TextBox4.Text +"','"+ DateTime.Now.ToString("yyyy-MM-dd"+"')" , sqlConnection1);
InBlock.gif                cmd1.ExecuteNonQuery();
InBlock.gif                sqlConnection1.Close();
InBlock.gif                Response.Write(
"<script language='javascript'>alert('增加成功');</script>");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else            
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//Response.Write("<script language='javascript'>alert('该帐号已经由其他职工揽收');</script>");
InBlock.gif                
//}
InBlock.gif                
//回车键代替TAB键
ExpandedSubBlockStart.gifContractedSubBlock.gif
                /**//*<script language="javascript">
InBlock.gif    function keyDown()
InBlock.gif    {
InBlock.gif    var keycode=event.keyCode;
InBlock.gif    if(keycode==13)
InBlock.gif    event.keyCode=9;
InBlock.gif    }
InBlock.gif    document.οnkeydοwn=keyDown;
ExpandedSubBlockEnd.gif        </script>
*/

InBlock.gif                sqlreader1.Close();
InBlock.gif                SqlCommand sqlcmd2 
= new SqlCommand("select zhigongming from lanchudengji where chuhuzhanghao='"+ TextBox2.Text +"'", sqlConnection1);
InBlock.gif                SqlDataReader sqlreader2 
= sqlcmd2.ExecuteReader();
InBlock.gif                sqlreader2.Read();
InBlock.gif                
string strZiGongXingMing = sqlreader2.GetString(0);
InBlock.gif                sqlreader2.Close();
InBlock.gif            
InBlock.gif                
if(DropDownList4.SelectedItem.Text == strZiGongXingMing)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    SqlCommand cmd2 
= new SqlCommand("insert into lanchudengji (zhigongming,suoshubumen,chuhuzhanghao,lanchujine,lanchuleixing,dakehu,cunruriqi,lururiqi) values ('"+ DropDownList4.SelectedItem.Text +"','"+ DropDownList1.SelectedItem.Value +"','" + TextBox2.Text + "','"+ TextBox3.Text +"','"+ DropDownList2.SelectedItem.Text +"','"+ DropDownList3.SelectedItem.Text +"','"+ TextBox4.Text +"','"+ DateTime.Now.ToString("yyyy-MM-dd"+"')" , sqlConnection1);
InBlock.gif                    cmd2.ExecuteNonQuery();
InBlock.gif                    sqlConnection1.Close();
InBlock.gif                    Response.Write(
"<script language='javascript'>alert('增加成功');</script>");
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    Response.Write(
"<script language='javascript'>alert('该帐号已经由其他职工揽收');</script>");
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif
InBlock.gif                TextBox2.Text
="";
InBlock.gif                TextBox3.Text
="";
InBlock.gif                TextBox4.Text
="";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*private void TextBox5_TextChanged(object sender, System.EventArgs e)
InBlock.gif        
InBlock.gif        {
InBlock.gif            for(int i=0,i<=DropDownList4.Items.Count-1,i++)
InBlock.gif            {
InBlock.gif                 if(DropDownList4.Items[i].Value==TextBox5.Text)
InBlock.gif                 {
InBlock.gif                 DropDownList4.SelectedIndex=i;
InBlock.gif                 }  
InBlock.gif            }
ExpandedSubBlockEnd.gif        }
*/

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif

转载于:https://www.cnblogs.com/cuancuan/archive/2006/01/26/323459.html

在 Java 中,要连接 MySQL 数据库需要使用 JDBC API。JDBC 提供了一组用于执行 SQL 命令的类和接口,可以通过这些类和接口来向数据库中插入数据,并且判断是否插入成功。下面是一个简单的示例: ```java import java.sql.*; public class Example { public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { // 连接数据库 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); // 执行插入数据的 SQL 命令 stmt = conn.createStatement(); String sql = "INSERT INTO users (name, age) VALUES ('Tom', 20)"; int rows = stmt.executeUpdate(sql); // 判断是否插入成功 if (rows > 0) { System.out.println("插入成功!"); } else { System.out.println("插入失败!"); } } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭数据库连接 try { if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } ``` 在上面的代码中,我们首先使用 `DriverManager.getConnection()` 方法来连接 MySQL 数据库。然后,使用 `conn.createStatement()` 方法创建一个 `Statement` 对象,用于执行 SQL 命令。接着,使用 `stmt.executeUpdate()` 方法执行插入数据的 SQL 命令,并返回受影响的行数。最后,根据受影响的行数判断是否插入成功。最后,我们使用 `finally` 块关闭 `Statement` 和 `Connection` 对象,释放资源。 需要注意的是,在实际使用中,我们应该使用 `PreparedStatement` 对象来执行 SQL 命令,以避免 SQL 注入攻击。另外,在连接数据库时,应该使用连接池来管理连接,以提高性能和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值