C#中怎样执行含有SQL语句的.sql文件啊?

xyusong 2005-05-11 03:06:57
我写了一些SQL语句并且把它放在了mysql.sql文件中,请问在C#中怎样执行我写在mysql.sql中的SQL语句啊?
谢谢le!!!
...全文
587 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
conan19771130 2005-05-20
  • 打赏
  • 举报
回复
先读出来,在执行那个
shanminmin 2005-05-20
  • 打赏
  • 举报
回复
sql文件不用在sql server服务器上,就用sqlcmmand执行就行了。还有你也可以直接调用执行
tsql 文件名.sql
这种方式。
realljx 2005-05-19
  • 打赏
  • 举报
回复
那如果我的SQL语句并不在SQL服务器上呢?
marvelstack 2005-05-12
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Collections;
using System.Xml;
using System.IO;
using System.Text;
using System.Diagnostics;

namespace Zhzuo
{

public class ZZConsole
{
[STAThread]
static void Main(string[] args)
{
//测试
//string vv = ExeCommand("dir");
//Console.WriteLine(vv);
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;
}
}
}
=====================
使用osql执行Sql语句。

用法: osql [-U login id] [-P password]
[-S server] [-H hostname] [-E trusted connection]
[-d use database name] [-l login timeout] [-t query timeout]
[-h headers] [-s colseparator] [-w columnwidth]
[-a packetsize] [-e echo input] [-I Enable Quoted Identifiers]
[-L list servers] [-c cmdend] [-D ODBC DSN name]
[-q "cmdline query"] [-Q "cmdline query" and exit]
[-n remove numbering] [-m errorlevel]
[-r msgs to stderr] [-V severitylevel]
[-i inputfile] [-o outputfile]
[-p print statistics] [-b On error batch abort]
[-X[1] disable commands [and exit with warning]]
[-O use Old ISQL behavior disables the following]
[-? show syntax summary]
具体参考
http://www.588188.com/netbook/sqlserver2000/coprompt/cp_osql_1wxl.htm
例如:
osql.exe /uSa /p123 /s192.192.132.229 /dNorthWind /i yoursql.sql



ms-help://MS.MSDNQTR.2003FEB.2052/vsintro7/html/vxwlkwalkthroughusingcustomactiontocreatedatabaseduringinstallation.htm
niunj 2005-05-11
  • 打赏
  • 举报
回复
对阿 肯定是在ado.net中运行这些相当于文本的一些命令而已。
chenyun122 2005-05-11
  • 打赏
  • 举报
回复
//从文件中提取SQL指令
private string GetSqlFile(string FilePath)
{
string input;
string Command
StreamReader sr;

sr = new StreamReader(FilePath,System.Text.Encoding.Default);
while ((input=sr.ReadLine())!=null)
{
Command = Command + " \n " + input;
}
sr.Close();
return Command;
}
skyeenet 2005-05-11
  • 打赏
  • 举报
回复
当成string 读成字符串 然后用 sqlcommand 执行不一样么?

110,545

社区成员

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

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

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