SQL语句参数化(1)插入数据

WebForm1.aspx.vb 
 
Imports  ...System.Text 
 
Public   Class WebForm1 
    
Inherits System.Web.UI.Page 
 
" Web 窗体设计器生成的代码 "Web 窗体设计器生成的代码 
    
Dim myconn As New Conn 
    
Dim mydata As New Getdata 
    
Dim sql As StringBuilder = New StringBuilder 
 
    
Private Sub Page_Load() Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
        mydata.myconn 
= New SqlClient.SqlConnection 
        mydata.myconn.ConnectionString 
= myconn.stringconnection 
        
If Not Page.IsPostBack Then 
        
End If 
    
End Sub
 
 
    
Private Sub Button1_Click() Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
 
        sql.Append(
"insert into chanpin("
        sql.Append(
"chanpin_name,chanpin_bianhao,chanpin_xiaotu,chanpin_putongjia,chanpin_kucun,chanpin_xiangxi"
        sql.Append(
")"
        sql.Append(
" values("
        sql.Append(
"@chanpin_name,@chanpin_bianhao,@chanpin_xiaotu,@chanpin_putongjia,@chanpin_kucun,@chanpin_xiangxi"
        sql.Append(
")"
 
        
Dim mypt As SqlClient.SqlParameter() = New SqlClient.SqlParameter() { _ 
        
New SqlClient.SqlParameter("@chanpin_name", SqlDbType.VarChar, 50), _ 
        
New SqlClient.SqlParameter("@chanpin_bianhao", SqlDbType.VarChar, 50), _ 
        
New SqlClient.SqlParameter("@chanpin_xiaotu", SqlDbType.VarChar, 1000), _ 
        
New SqlClient.SqlParameter("@chanpin_putongjia", SqlDbType.Decimal, 9), _ 
        
New SqlClient.SqlParameter("@chanpin_kucun", SqlDbType.Int, 4), _ 
        
New SqlClient.SqlParameter("@chanpin_xiangxi", SqlDbType.Text)} 
 
        mypt(
0).Value = TextBox1.Text 
        mypt(
1).Value = TextBox2.Text 
        mypt(
2).Value = TextBox3.Text 
        mypt(
3).Value = TextBox4.Text 
        mypt(
4).Value = TextBox5.Text 
        mypt(
5).Value = TextBox6.Text 
 
        mydata.myconn.Open() 
        mydata.executesql(sql.ToString, mypt) 
        
' mydata.executesql(sql.ToString, Nothing) '不需要用到参数时,传nothing 
 
        mydata.myconn.Close() 
 
    
End Sub
 
End Class
  
 
  
 
Getdata.vb 
 
Public   Class Getdata 
 
    
Public myconn As SqlClient.SqlConnection 
 
    
Public Function executesql() Function executesql(ByVal sql As StringByVal myPt As SqlClient.SqlParameter()) 
        
Dim myCmd As SqlClient.SqlCommand 
        myCmd 
= New SqlClient.SqlCommand(sql, myconn)               ' 存储过程时sql应改为存储过程名 
        ' myCmd.CommandType = CommandType.StoredProcedure ' 存储过程时需要添加该语句       
        If IsNothing(myPt) Then 
            myCmd.ExecuteNonQuery() 
        
Else 
            
Dim mypt1 As SqlClient.SqlParameter 
            
For Each mypt1 In myPt 
                myCmd.Parameters.Add(mypt1) 
            
Next 
            myCmd.ExecuteNonQuery() 
            myCmd.Parameters.Clear() 
        
End If  
End Function
 
   
End Class
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值