您的位置:首页 > 数据库

C#中如何执行sql脚本?

2007-05-30 14:23 232 查看
在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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: