Avatar of xpert_ali
xpert_ali
Flag for India asked on

Recognize SQL Queries from TextBox and Execute Separately

Hi

I am doing a project where user enters SQL Queries like in SQL Management Editor . Many Individual queries ..

Here even i am doing the same . If user writes 2 or more queries  in a text box they all needs to be recognized separately Like individual queries and needs be executed separately.

Regards
Avin
Query.bmp
.NET ProgrammingMicrosoft SQL Server

Avatar of undefined
Last Comment
Vitor Montalvão

8/22/2022 - Mon
hafeezmca

HI, try this on the button event:

Dim conn As SqlConnection
        Dim cmd As New SqlCommand
        Dim dr As SqlDataReader
        Dim i, j As Integer
        Dim qry(10) As String ' Say max 10 queries
        Dim str1 As String
        str1 = Me.TextBox3.Text
        i=0
        While Len(str1) > 5
            qry(i) = Mid$(UCase(str1), 1, InStr(str1, Chr(13)))
            str1 = Mid$(str1, InStr(str1, Chr(13)) + 1, Len(str1))
            i = i + 1
        End While
        conn = New SqlConnection(ConfigurationManager.ConnectionStrings("YourConnectionString").ConnectionString)
        For j = 0 To 9
            cmd.CommandText = qry(j)
            dr = cmd.ExecuteReader
            While dr.Read
                Response.Write("Executing query")
            End While
            dr.close

        Next
        cmd.close
        conn.Close()

Havent tested it, but guess it should work properly.
xpert_ali

ASKER
Hi

See the text box might contain 1st query which might have length of 15 lines and 2nd with 2 lines and 3rd with 1 line .

But while executing currently i am using directly textbox, that is wrong . I want to know how can i detect 3 queries written and differentiate them . Executing part already i have done with that.

Now only i need to find how many individual queries are there for which the output needs to be shown separately .

Regards
Avin
hafeezmca

The solution which I have posted works if Enter is pressed after each query and doesnt matter if its single line or 15 lines.
Else you may ask the users to seperate the queries by semicolon and modify the above code accordingly.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
xpert_ali

ASKER
Hi

Sorry Have Checked the code . It wont return any query in the array, Just it will be filled with space for every index and Even throws Array out of index exception after completing 9th while condition .

Regards
Avin
ASKER CERTIFIED SOLUTION
Vitor Montalvão

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question