怎么用C# 直接执行sql server 2000数据库脚本文件?

csdnshao 2004-10-20 04:55:36
请教
...全文
258 15 打赏 收藏 转发到动态 举报
写回复
用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

110,567

社区成员

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

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

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