`
caozuiba
  • 浏览: 902904 次
文章分类
社区版块
存档分类
最新评论

C#中如何执行sql脚本?

 
阅读更多
<iframe marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>

在C#中执行SQL脚本,可以考虑使用osql工具。
Example :
#region 调用Osql.exe执行建库脚本
/// <summary>
/// 调用Osql.exe执行建库脚本
/// </summary>
/// <param name="UserName">数据库访问用户名</param>
/// <param name="Pwd">数据库访问密码</param>

private void CreateDataBase ()
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string Path = Application.StartupPath.ToString();
string Parameter = "osql.exe -U " + uid + " -P " + pwd + " -S "+ ServerName +" -i " + Path + @"/IPMS.sql";
try
{
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
p.Start();
p.StandardInput.WriteLine(Parameter);
p.StandardInput.WriteLine("exit");
p.StandardInput.WriteLine("exit");
p.WaitForExit();
p.Close();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
this.Close();
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics