excute sql in .net

  VB:

          'To represent the table parameter in C#, we need to either 
            'have a set of entities which are IEnumreable 
            'or a data reader or a Data table.
            'In this example we create a data table with same name as the type we have in the DB 
            Dim dataTable As System.Data.DataTable = New System.Data.DataTable("SampleDataType")
            'we create column names as per the type in DB 
            dataTable.Columns.Add("LocationName", GetType(String))
            dataTable.Columns.Add("CostRate", GetType(Int32))
            'and fill in some values 
            dataTable.Rows.Add("99", 99)
            dataTable.Rows.Add("98", Nothing)
            dataTable.Rows.Add("97", 99)
            Dim conn As New Data.SqlClient.SqlConnection
            Dim cmd As New Data.SqlClient.SqlCommand
            If conn.State = Data.ConnectionState.Open Then
                conn.Close()
            End If
            conn.ConnectionString = "Integrated Security=SSPI;Persist Security Info=True;Data Source=localhost;Packet Size=4096;Initial Catalog=AdventureWorks2008"
            conn.Open()
            Dim parameter As System.Data.SqlClient.SqlParameter = New System.Data.SqlClient.SqlParameter()
            'The parameter for the SP must be of SqlDbType.Structured 
            parameter.ParameterName = "@TVP"
            parameter.SqlDbType = System.Data.SqlDbType.Structured
            parameter.Value = dataTable
            With cmd
                .Connection = conn
                .CommandType = Data.CommandType.StoredProcedure
                .CommandText = "usp_InsertProductionLocation"
                '.Parameters.Add(New Data.SqlClient.SqlParameter("@txta", Data.SqlDbType.VarChar, 10))
                '.Parameters("@txta") = txta.text
                '.Parameters.Add(New Data.SqlClient.SqlParameter("@txtb", Data.SqlDbType.VarChar, 10))
                '.Parameters("@txtb") = txtb.text
                .Parameters.Add(parameter)
                grdDetail.DataSource = .ExecuteReader()
            End With
            'Dim daGetProducts As New Data.SqlClient.SqlDataAdapter(cmd)
            'Dim dsProducts As New Data.DataSet()
            'daGetProducts.Fill(dsProducts, "Products")
            'grdDetail.DataSource = dsProducts
            'GridView1.DataSource = dsProducts
            'GridView1.DataBind()
            grdDetail.DataBind()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值