10月 20, 2013

Asp.Net-VB

Imports System.Data.SqlClient
Imports System.Data

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim hh = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        Dim conn As New SqlConnection(hh)
        conn.Open()
        Dim Trans As SqlTransaction = conn.BeginTransaction()
        Dim sql = "Insert into stu(stu,c1,c2) values(null,'hello','kg')"
        Dim cmd As New SqlCommand(sql, conn, Trans)
        sql = "update the set t1='abcd' where the = 102"
        Dim cmd2 As New SqlCommand(sql, conn, Trans)
        Try

            cmd.ExecuteNonQuery()
            cmd2.ExecuteNonQuery()
            Trans.Commit()

        Catch ex As Exception

            Trans.Rollback()

        End Try

        cmd.Dispose()
        cmd2.Dispose()
        conn.Close()
        conn.Dispose()

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sds1 As SqlDataSource = New SqlDataSource
        sds1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        sds1.SelectCommand = "SELECT * FROM [stu]"
        Dim dv As DataView = sds1.Select(DataSourceSelectArguments.Empty)
        GridView1.DataSource = dv
        GridView1.DataBind()
        sds1.Dispose()

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sds1 As SqlDataSource = New SqlDataSource
        sds1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        sds1.SelectCommand = "SELECT * FROM [stu]"
        Dim dv As DataView = sds1.Select(DataSourceSelectArguments.Empty)
        Label1.Text = dv.Table.Rows(0)(0)
        GridView1.Rows(0).Cells(2).Text = dv.Table.Rows(0)(1)
        sds1.Dispose()

    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim jj = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        Dim cn As New SqlConnection(jj)
        Dim cmd As New SqlCommand("SELECT * FROM [stu]", cn)
        cn.Open()
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        dr.Read()
        Label1.Text = dr(1).ToString
        dr.Close()
        cmd.Dispose()
        cn.Close()
        cn.Dispose()


    End Sub

    Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim sds1 As SqlDataSource = New SqlDataSource
        sds1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        sds1.InsertCommand = "Insert into stu(stu,c1,c2) values(119,@c1,'kg')"
        'sds1.InsertParameters.Add("@c1", SqlDbType.Int)
        sds1.InsertParameters.Add(New Parameter("c1", DbType.String, TextBox1.Text))
        'sds1.InsertParameters("c1").DefaultValue = TextBox1.Text
        sds1.Insert()
        sds1.SelectCommand = "SELECT * FROM [stu]"
        Dim dv As DataView = sds1.Select(DataSourceSelectArguments.Empty)
        GridView1.DataSource = dv
        GridView1.DataBind()
        sds1.Dispose()


        Label1.Text = GridView1.Rows(0).Cells(1).Text
    End Sub

    Protected Sub Button5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim hh = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        Dim conn As New SqlConnection(hh)
        Dim cmd As New SqlCommand("spss", conn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@c2", SqlDbType.Text).Value = TextBox1.Text

        Dim out As SqlParameter = cmd.Parameters.Add("@c3", SqlDbType.NVarChar, 50)
        out.Direction = ParameterDirection.Output

        Dim ret As SqlParameter = cmd.Parameters.Add("@ret", SqlDbType.Int)
        ret.Direction = ParameterDirection.ReturnValue
        conn.Open()
        cmd.ExecuteNonQuery()
        Label1.Text = out.Value.ToString
        TextBox1.Text = ret.Value.ToString
        cmd.Dispose()

        conn.Close()
        conn.Dispose()

        Dim sds1 As SqlDataSource = New SqlDataSource
        sds1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        sds1.SelectCommand = "SELECT * FROM [stu]"
        Dim dv As DataView = sds1.Select(DataSourceSelectArguments.Empty)
        GridView1.DataSource = dv
        GridView1.DataBind()
        sds1.Dispose()
    End Sub

    Protected Sub Button6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim sds1 As SqlDataSource = New SqlDataSource
        sds1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        sds1.SelectCommand = "spss"
        sds1.SelectCommandType = SqlDataSourceCommandType.StoredProcedure
        sds1.SelectParameters.Add(New Parameter("c2", DbType.String, TextBox1.Text))
        sds1.SelectParameters.Add(New Parameter("c3", DbType.String, "null"))
        'sds1.Select(DataSourceSelectArguments.Empty)
        Dim dv As DataView = sds1.Select(DataSourceSelectArguments.Empty)
        GridView2.DataSource = dv
        GridView2.DataBind()
        sds1.Dispose()
    End Sub

    Protected Sub Button7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim hh = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        Dim conn As New SqlConnection(hh)
        Dim cmd As New SqlCommand("spss", conn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@c2", SqlDbType.Text).Value = TextBox1.Text

        Dim out As SqlParameter = cmd.Parameters.Add("@c3", SqlDbType.NVarChar, 50)
        out.Direction = ParameterDirection.Output

        Dim ret As SqlParameter = cmd.Parameters.Add("@ret", SqlDbType.Int)
        ret.Direction = ParameterDirection.ReturnValue
        conn.Open()
        'Dim num As String = cmd.ExecuteScalar
        Dim dvi As SqlDataReader = cmd.ExecuteReader
        dvi.Read()
        dvi.Read()
        'Label1.Text = out.Value.ToString
        'TextBox1.Text = ret.Value.ToString
        Label1.Text = dvi(1).ToString
        cmd.Dispose()

        conn.Close()
        conn.Dispose()

        Dim sds1 As SqlDataSource = New SqlDataSource
        sds1.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("sp").ConnectionString
        sds1.SelectCommand = "SELECT * FROM [stu]"
        Dim dv As DataView = sds1.Select(DataSourceSelectArguments.Empty)
        GridView1.DataSource = dv
        GridView1.DataBind()
        sds1.Dispose()
    End Sub
End Class

沒有留言: