10月 21, 2013

SQL-stored procedure

ALTER PROCEDURE [dbo].[spss] 
    -- Add the parameters for the stored procedure here
    @c2 nvarchar(50),
    @c3 nvarchar(50) output
    --DECLARE @oo nchar(20) = 'Transaction1';
AS
BEGIN TRY
BEGIN TRAN m1
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    -- SET NOCOUNT ON;
    Insert into stu(stu,c1,c2) values(121,'hello',@c2)
    update the set t1='www' where the = 102
    -- Insert statements for procedure here
    set @c3 = @c2
    SELECT * FROM [stu]
    
COMMIT TRAN m1
return 999
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
     ROLLBACK TRAN m1
     -- Return the error information.
  DECLARE @ErrorMessage nvarchar(4000),  @ErrorSeverity int;
  SELECT @ErrorMessage = ERROR_MESSAGE(),@ErrorSeverity = ERROR_SEVERITY();
  RAISERROR(@ErrorMessage, @ErrorSeverity, 1);
END CATCH

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

10月 11, 2013

從使用中儲存格後開始尋找含有

Sub Click()
    
moji = "大佑"

'MsgBox "從使用中儲存格後開始尋找含有" & moji & "文字的儲存格"

Set iti = Cells.Find(What:=moji, After:=ActiveCell)
    
If iti Is Nothing Then
MsgBox "找不到您想要找的資料"
Exit Sub
Else
iti.Activate
End If
    
End Sub