mohamed ajmeer

mohamed ajmeer

  • NA
  • 28
  • 61.8k

how to execute sql query from text file using c#.net 2008

Apr 10 2011 3:38 AM
SQL QUERY:
...............................
 
CREATE
DATABASE [finaltest12]
go
CREATE
TABLE finaltest12.dbo.stud(name varchar(20))
go
insert
into finaltest12.dbo.stud values('ajm')
go
.....................................................................
 
The above SQL query iam saved in text file(test.txt) in c#2008
if i call and execute the programe, the error come on ''go'' ,how can i call and execute the query through c#2008.
 
my c#.net programe:
.....................................
 
protected void Button1_Click1(object sender, EventArgs e)
{
String createQuery;
SqlConnection con = new SqlConnection("server= JOMANA-PC\\SQLEXPRESS; user id= sa; password= ; ");
string filename="C:\\Users\\Jomana\\Documents\\Visual Studio 2008\\Projects\\createdatabase\\test.txt";
if (System.IO.File.Exists(filename) == true)
{
System.IO.
StreamReader objreader;
objreader =
new System.IO.StreamReader(filename);
createQuery = objreader.ReadToEnd();
objreader.Close();
SqlCommand myCommand = new SqlCommand(createQuery, con);
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
Response.Write(
"<script>alert('Database Created')</script>");
//Response.Write("Database Created");
}
else
{
Response.Write(
"File Name Not Found");
}
....................................................