Wednesday, February 15, 2012

Execute Create table command from asp.net

I have a little application that I have designed where I need to be able to execute create table and create function comands against the database.

It seems that it does not like my sql file. Does anyone know of a different method of doing this?

Error message

Line 2: Incorrect syntax near 'GO'.

Line 4: Incorrect syntax near 'GO'.

Line 8: Incorrect syntax near 'GO'.

'CREATE FUNCTION' must be the first statement in a query batch.

Must declare the variable '@.usb'.

Must declare the variable '@.usb'.

Must declare the variable '@.i'.

A RETURN statement with a return value cannot be used in this context.

Line 89: Incorrect syntax near 'GO'.

Line 91: Incorrect syntax near 'GO'.

Line 94: Incorrect syntax near 'GO'.

ProtectedSub Install() Dim errAsString =""While err.Length < 1' Dim your StreamReader Dim TextFileStreamAs System.IO.TextReader 'Load the textfile into the stream

TextFileStream = System.IO.File.OpenText(Request.PhysicalApplicationPath &

"Scripts\0.sql") 'Read to the end of the file into a String variable.

executesql(TextFileStream.ReadToEnd, err)

err =

"Susscessful"EndWhileIf err ="Susscessful"Then

Response.Redirect(

"Default.aspx")Else Me.lblError.Text = errEndIfEndSubPrivateFunction executesql(ByVal sAsString,ByRef errAsString)AsBooleanTry Dim connAsNew Data.SqlClient.SqlConnection(GenConString()) Dim cmdAsNew Data.SqlClient.SqlCommand(s, conn)

conn.Open()

cmd.ExecuteNonQuery()

conn.Close()

ReturnTrue Catch exAs Exception

err = ex.Message.ToString

ReturnFalse

EndTry

EndFunction

Example sql file

SET QUOTED_IDENTIFIER ON

GO

SET

ANSI_NULLSON

GO

if exists

(select*fromdbo.sysobjectswhereid =object_id(N'[dbo].[MyFunc]')andxtypein(N'FN', N'IF', N'TF'))

drop

function [dbo].[MyFunc]

GO

CREATE

FUNCTION [dbo].[MyFunc]

(

-- Add the parameters for the function here

)

RETURNS

varchar(1000)

AS

BEGIN

-- Declare the return variable hereDECLARE@.Resultvarchar(1000)-- Add the T-SQL statements to compute the return value here-- Do something here-- Return the result of the functionRETURN@.Result

END

GO

SET QUOTED_IDENTIFIER OFF

GO

SET

ANSI_NULLSON

GO

You cannot use ExecuteNonQuery to run a batch of SQL commands. Try using the OSQL command utility instead.

No comments:

Post a Comment