社区
C#
帖子详情
C#中怎样执行含有SQL语句的.sql文件啊?
xyusong
2005-05-11 03:06:57
我写了一些SQL语句并且把它放在了mysql.sql文件中,请问在C#中怎样执行我写在mysql.sql中的SQL语句啊?
谢谢le!!!
...全文
626
7
打赏
收藏
C#中怎样执行含有SQL语句的.sql文件啊?
我写了一些SQL语句并且把它放在了mysql.sql文件中,请问在C#中怎样执行我写在mysql.sql中的SQL语句啊? 谢谢le!!!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用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 执行不一样么?
如何在
c#
代码
中
执行
带GO语句的
SQL
文件
如何在
c#
代码
中
执行
带GO语句的
SQL
文件
需要在
C#
代码
中
执行
一个
SQL
文件
的内容。遇到了两个问题: 1. 因为
SQL
文件
中
有“GO”语句,
执行
时报错“Incorrect syntax near 'GO'.”。 2. 如果将
SQL
文件
中
有“GO”语句全部去掉,则可能出现
SQL
运行错误。原因是去掉“GO”后,等于整个
文件
的内容一次
执行
,但是
SQL
文件
有可能后
如何用
C#
程序直接
执行
.
sql
文件
里的
SQL
语句
把写在.
sql
里面的语句取出(用读取文本
文件
的方法),构造成
sql
语句
, 1.用
Sql
Command
执行
;2.将构造成的
sql
语句
存入数据库表
中
,写触发器
执行
; 取文本
文件
中
的内容,可用System.IO.File 里面的方法 if (File.Exists(FILE_NAME)) //如果
文件
存在 { String[] ...
C#
读取*.
sql
文件
,并
执行
里面的
SQL
语句
public static ArrayList Get
Sql
File(string varFileName, string dbname) { ArrayList al
Sql
= new ArrayList(); if (!File.Exists(varFileName)) {
C#
111,129
社区成员
642,541
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章