设计一个winform程序,使其能够读取选择的sql脚本并自动执行脚本

如题所述

跟读 文本文件一样
读出来 执行就可以了
private List<string> GetTsqlsFormSqlFile(string varFileName)
{
StreamReader sr = new StreamReader(varFileName, Encoding.GetEncoding("GB2312"));
List<string> alSql = new List<string>(); //每读取一条语名存入ArrayList
string commandText = "";
string varLine = "";
while (sr.Peek() > -1)
{
varLine = sr.ReadLine();
if (varLine == "")
{
continue;
}
if (varLine.ToUpper() != "GO")
{
commandText += varLine;
commandText += " ";
}
else
{
alSql.Add(commandText);
commandText = "";
}
}
sr.Close();
return alSql;
}追问

有示例代码么,可以用来参考的

追答

注释什么的 这里没管

追问

不知道是不是我没有看懂,这个好像只是把脚本读取吧,有没有执行呢

追答

?? 跟增删改 什么的 不都一样吗? 语句都有了 套一下 不就可以吗?

追问

我需要的就是后面执行的部分。。。。

追答

foreach(string sqlstr in GetTsqlsFormSqlFile("D:\\111.sql"))
{
SqlCommand cmd = new SqlCommand(sqlstr, this.sqlcon);
cmd.CommandType = CommandType.Text;
this.sqlcon.Open();
int count = cmd.ExecuteNonQuery();
this.sqlcon.Close();
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2011-06-13
你是不是想选择一个sql语句之后,界面出现相应查询的结果啊?追问

比如现在我有一个sql更新脚本,我希望通过这个程序让对应的数据库执行脚本内容,自动完成更新

追答

你的意思是类似于数据库引擎的功能,这个还不会。

追问

就是读取脚本的每一行,然后执行,其他的我也说不清楚。。。。还是谢谢你了

相似回答