.net下执行SQL文件脚本
来源:互联网 发布:https www.java.com 编辑:程序博客网 时间:2024/05/28 03:01
下面是自己凑的代码,需要建立数据源,可以执行,有没有不需要设置数据源的方法?
因为我这里需要在客户机上执行.sql文件,所以需要一个可以直接执行.SQL的小工具。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Diagnostics;
namespace DoSQL
{
public partial class FrmSQL : Form
{
public FrmSQL()
{
InitializeComponent();
}
string file=string.Empty;
private void btnQuery_Click(object sender, EventArgs e)
{
SelectSQLFile();
}
private void SelectSQLFile()
{
string fileName = string.Empty;
string exName = string.Empty;
this.openFileDialog1.Filter = "SQL 脚本|*.sql";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = this.openFileDialog1.FileName;
exName = Path.GetExtension(fileName);
if (exName.ToLower() == ".sql")
{
this.txtSQL.Text = fileName;
file = fileName;
}
else
{
MessageBox.Show("必须选择.sql格式文件!");
}
}
}
private void btnLoad_Click(object sender, EventArgs e)
{
string SerName = string.Empty;
string DatName = string.Empty;
string Sid = string.Empty;
string Psw = string.Empty;
if (this.txtServerName.Text == "" && this.txtDataBaseName.Text == "")
{
MessageBox.Show("请输入服务器名和数据源名!");
return;
}
if (file == "")
{
MessageBox.Show("请先选择.sql文件!");
return;
}
try
{
SerName = this.txtServerName.Text.ToString().Trim();
DatName = this.txtDataBaseName.Text.ToString().Trim();
Sid = this.txtName.Text.ToString().Trim();
Psw = this.txtPassW.Text.ToString().Trim();
string sqlQuery = "osql -U " + Sid + " -P " + Psw + " -S " + SerName + " -D " + DatName + " -i " + file + "";
//osql方式必须与ODBC数据源合用,所以使用的机器上必须添加ODBC数据源,而且要注意选择默认数据库
string strRst = ExeCommand(sqlQuery);
Console.WriteLine(strRst);
Console.ReadLine();
}
catch (Exception err)
{
MessageBox.Show(err.ToString ());
}
}
public static string ExeCommand(string commandText)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
string strOutput = null;
try
{
p.Start();
p.StandardInput.WriteLine(commandText);
p.StandardInput.WriteLine("exit");
strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
}
catch (Exception e)
{
strOutput = e.Message;
}
return strOutput;
}
}
}
- .net下执行SQL文件脚本
- .net执行SQL脚本文件
- MySql下如何执行sql脚本文件
- 执行sql脚本文件
- 如何在java环境下执行sql脚本文件
- PL/SQL Developer 下执行oracle的脚本文件
- PL/SQL Developer 下执行oracle的脚本文件
- Windows下MySQL批量执行SQL脚本文件
- Windows下MySQL批量执行SQL脚本文件
- .NET执行sql文件
- mysql 下执行sql脚本
- linux下执行sql脚本
- ubuntu下执行sql脚本
- ADO执行SQL脚本文件
- Java 执行 SQL 脚本文件
- 执行SQL语句脚本文件
- Java 执行 SQL 脚本文件
- Java执行SQL脚本文件
- 如何在c#代码中执行带GO语句的SQL文件
- Linux内核/模块开发常见问题集(FAQ)
- Asp.net中基于Forms验证的角色验证授权
- 近来二三事 - 20090316
- Excel表格中获取或删除超链接的方法
- .net下执行SQL文件脚本
- SQL表解锁步骤
- 近来二三事 - 20090327
- WCDMA呼叫流程总结zz
- c#自己做了个邮件发送程序~~呵呵,很好玩。。。。
- 不是花儿不美,只怪心情糟糕
- Linux下top命令参数解释
- 寻求自动化测试之道 —— 我的困惑与期望
- 将Derby数据库与birt 集成到RCP