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();
}
}
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();
}
}
相关文章推荐
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库.txt
- 如何在C#里面执行.SQL脚本(SQL SERVER)
- C#中如何执行sql脚本?
- C#中如何执行sql脚本?
- 如何在C#里面执行.SQL脚本(SQL SERVER)
- 如何在C#里面执行.SQL脚本(SQL SERVER)(转)
- 如何执行一个mysql的sql脚本文件
- C#执行SQL脚本文件
- C#执行SQL脚本文件
- MySql下如何执行sql脚本文件
- mysql下如何执行sql脚本
- 如何在c#代码中执行带GO语句的SQL文件
- 如何使用T-SQL备份还原数据库及c#如何调用执行?
- C# 执行SQL脚本文件
- mysql下如何执行sql脚本
- mysql下如何执行sql脚本
- MySql下如何执行sql脚本文件
- mysql下如何执行sql脚本
- 如何在VS里的部署中执行一段 .sql 的脚本文件?