C#怎样才能执行.sql文件的内容?

ls176 2005-10-28 04:50:04
我在.sql文件中写了两个创建函数的批处理命令,然后用streamreader读入到程序中,但是在用command的Execute方法时,程序不认"go"这个字符,两个函数分开执行都没有问题,难到我要放到两个文件中去吗?
...全文
274 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
marvelstack 2005-10-29
  • 打赏
  • 举报
回复
using System;

using System.Data;

using System.Collections;

using System.Xml;

using System.IO;

using System.Text;

using System.Diagnostics;



namespace ZZ

{

public class ZZConsole

{

[STAThread]

static void Main(string[] args)

{

string sqlQuery = "osql.exe /uSa /p123 /s192.192.132.229 /dNorthWind /i yoursql.sql";

string strRst = ExeCommand(sqlQuery);

Console.WriteLine(strRst);

Console.ReadLine();

}



public static string ExeCommand(string commandText)

{

Process p = new Process();

p.StartInfo.FileName = "cmd.exe";

p.StartInfo.UseShellExecute = false;

p.StartInfo.RedirectStandardInput = true;

p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.RedirectStandardError = true;

p.StartInfo.CreateNoWindow = true;

string strOutput = null;

try

{

p.Start();

p.StandardInput.WriteLine(commandText);

p.StandardInput.WriteLine("exit");

strOutput = p.StandardOutput.ReadToEnd();

p.WaitForExit();

p.Close();

}

catch(Exception e)

{

strOutput = e.Message;

}

return strOutput;

}

}

}

更多 可以看,
http://blog.csdn.net/zhzuo/archive/2004/12/25/229006.aspx
diablo1003 2005-10-29
  • 打赏
  • 举报
回复
mark
singlepine 2005-10-28
  • 打赏
  • 举报
回复
GO is not a Transact-SQL statement; it is a command recognized by the osql and isql utilities and SQL Query Analyzer.

go只是查询分析器的一个命令,而不是sql的命令,在sql中包含go是会出错的
wangsaokui 2005-10-28
  • 打赏
  • 举报
回复
如何读取文件你应该知道了。
string str_sql= "......"; //.sql文件中的具体内容
SqlCommand cmd = new SqlCommand(str_sql,conn);
conn.Open();
cmd.ExecuteNoQuery();
conn.Close();
只需要去掉你的sql文件中的go
存储过程的上面有例子了。
ls176 2005-10-28
  • 打赏
  • 举报
回复
我的意思是问我怎样可以让程序执行.sql文件中的内容,就象PowerDesign中把表都建好后,连上数据库再执行生成好的sql批处理命令
wangsaokui 2005-10-28
  • 打赏
  • 举报
回复
SqlClient.SqlCommand backUpDB = new SqlClient.SqlCommand();
backUpDB.CommandType = CommandType.StoredProcedure;
backUpDB.CommandText = "BackUpDBSproc";
backUpDB.Connection = SqlConnection1;

SqlConnection1.Open();
backUpDB.ExecuteNonQuery();
SqlConnection1.Close();
oolongTea 2005-10-28
  • 打赏
  • 举报
回复
每一个"Go"单独占一行
以"GO"为判别条件来判断每个SQL语句的起始
jxufewbt 2005-10-28
  • 打赏
  • 举报
回复
同意wangsaokui(无间道III(MSMVP))
overdream 2005-10-28
  • 打赏
  • 举报
回复
頂一下,學習中。
wangsaokui 2005-10-28
  • 打赏
  • 举报
回复
1 做成存储过程,然后调用
2 去掉go,改为SqlCommand.ExecuteNoQuery()

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧