社区
C#
帖子详情
怎么用C# 直接执行sql server 2000数据库脚本文件?
csdnshao
2004-10-20 04:55:36
请教
...全文
258
15
打赏
收藏
怎么用C# 直接执行sql server 2000数据库脚本文件?
请教
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
15 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
csdnshao
2004-10-26
打赏
举报
回复
谢谢大家:
System.Diagnostics.Process.Start("OSQL", "-S(local) -Usa -Ps -i1.sql");
csdnshao
2004-10-23
打赏
举报
回复
没有人会了吗?
csdnshao
2004-10-21
打赏
举报
回复
运行OSQL客户端怎么用?
有没有别的办法?
brightheroes
2004-10-21
打赏
举报
回复
那就只好去运行OSQL了
那还怎么办
没有"GO"的话就不是SQLSERVER的SQL脚本呀
你可以写多种情况
为了通用
呵呵
自己定义一些SPLITER也可以
自己把握了
csdnshao
2004-10-21
打赏
举报
回复
to brightheroes(闭关|那一剑的风情) :
这样的方法我也写过,现在就是这么做的。
总觉得不专业、不通用。
csdnshao
2004-10-21
打赏
举报
回复
to brightheroes(闭关|那一剑的风情) :
如果文件中没有"GO"怎么办?
jdk152
2004-10-20
打赏
举报
回复
都是高手
ncowboy
2004-10-20
打赏
举报
回复
我还是很喜欢osql的。
//System.Diagnostics.Process.Start("osql", "-q sqlstring");
客户机上没有osql怎么办啊?
brightheroes
2004-10-20
打赏
举报
回复
DBAccess ---这是个什么?
是上面我给你贴的类啊,呵呵
BTW:我不喜欢用OSQL,感觉太慢!
heroqxn
2004-10-20
打赏
举报
回复
System.Diagnostics.Process.Start("osql", "-q sqlstring");
csdnshao
2004-10-20
打赏
举报
回复
DBAccess ---这是个什么?
brightheroes
2004-10-20
打赏
举报
回复
调用
DBAccess.ExecuteSqlFile(@"E:\My Document\MySql\test1.sql");
brightheroes
2004-10-20
打赏
举报
回复
<?xml version="1.0" encoding="utf-8" ?>
<ServerConfig>
<ServerName>localhost</ServerName>
<DataBase>tttttt</DataBase>
<UserId>sa</UserId>
<PassWord>sa</PassWord>
</ServerConfig>
放到你的程序的BIN目录下面,保存为ServerConfig.xml
brightheroes
2004-10-20
打赏
举报
回复
using System;
using System.Xml;
using System.Data;
using System.IO;
using System.Collections;
using System.Data.SqlClient;
namespace ExecuteSqlFile
{
/// <summary>
/// DBAccess 的摘要说明。
/// </summary>
public class DBAccess
{
public DBAccess()
{
}
#region 属性
private static string ConStr = "";
private static string ConString
{
get
{
if(ConStr == "")
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load("ServerConfig.xml");
string userid = doc.SelectSingleNode("ServerConfig/UserId").InnerText;
string password = doc.SelectSingleNode("ServerConfig/PassWord").InnerText;
string servername = doc.SelectSingleNode("ServerConfig/ServerName").InnerText;
string database = doc.SelectSingleNode("ServerConfig/DataBase").InnerText;
ConStr = "server = " + servername + ";uid = "
+ userid + ";pwd = " + password + ";database = " + database;
}
catch(Exception ex)
{
throw ex;
}
}
return ConStr;
}
}
private static SqlConnection Con;
public static SqlConnection MyConnection
{
get
{
if(Con == null)
{
Con = new SqlConnection(ConString);
}
return Con;
}
}
#endregion
/// <summary>
/// 执行Sql文件
/// </summary>
/// <param name="varFileName"></param>
/// <returns></returns>
public static bool ExecuteSqlFile(string varFileName)
{
if(!File.Exists(varFileName))
{
return false;
}
StreamReader sr = File.OpenText(varFileName);
ArrayList alSql = new ArrayList();
string commandText = "";
string varLine = "";
while(sr.Peek() > -1)
{
varLine = sr.ReadLine();
if(varLine == "")
{
continue;
}
if(varLine != "GO")
{
commandText += varLine;
commandText += "\r\n";
}
else
{
alSql.Add(commandText);
commandText = "";
}
}
sr.Close();
try
{
ExecuteCommand(alSql);
}
catch
{
return false;
}
return true;
}
private static void ExecuteCommand(ArrayList varSqlList)
{
MyConnection.Open();
SqlTransaction varTrans = MyConnection.BeginTransaction();
SqlCommand command = new SqlCommand();
command.Connection = MyConnection;
command.Transaction = varTrans;
try
{
foreach(string varcommandText in varSqlList)
{
command.CommandText = varcommandText;
command.ExecuteNonQuery();
}
varTrans.Commit();
}
catch(Exception ex)
{
varTrans.Rollback();
throw ex;
}
finally
{
MyConnection.Close();
}
}
}
}
csdnshao
2004-10-20
打赏
举报
回复
up
C#
获取
SQL
server
数据库
存储过程
脚本
,可重复
执行
C#
获取
SQL
server
数据库
存储过程
脚本
,可重复
执行
.用于给
数据库
升级的打升级包的时候,十分方便
C#
.
sql
文件
执行
sql
server
数据库
安装(源码)
C#
.
sql
文件
执行
sql
server
数据库
安装(源码).如果
数据库
连接参数无效,则弹出参数设置向导;配置成功后再运行建库
脚本
。建库
脚本
文件
名称及路径保存在与
执行
程序同名的配置
文件
(.config)中。代码测试过无问题,拿来就...
C#
SQL
Server
数据库
技术
本课程讲解
C#
对
SQL
SERVER
数据库
的操作,包括
数据库
的连接、
数据库
的增、删、改、查询等。
SQL
小工具 批量
执行
SQL
脚本
文件
(WinForm开发 .Net 2.0)
1、批量
执行
SQL
脚本
文件
; 2、
文件
拷贝操作。 其中,App.config为工具配置
文件
, <?xml version="1.0" encoding="utf-8" ?> <add key="DbConnectstring" value="
SERVER
= .;DataBase = master;User ID=sa; ...
c#
直接
运行.
sql
脚本
必须的dll
sql
dll
c#
脚本
C#
110,567
社区成员
642,567
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章