c#执行sql脚本的问题

我希望写个程序,调用cmd.exe执行sqlplus,然后执行指定的.sql脚本,数据库是oracle9i的。请问c#中应该怎么实现?

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.SqlClient;

namespace openfile
{
class Program
{
static void Main(string[] args)
{
baseclass bs=new baseclass();
System.Console.WriteLine("请输入路径(例'c://cmd.sql')");//输入你要执行文件的目录
string path=System.Console.ReadLine();
string sql=bs.sql(path);
int i = 0;
SqlConnection con = new SqlConnection("server=.;database=oracle9i;user id=sa;password=123");//可以根据你自己的机子设置的数据库,用户名,密码而定
SqlCommand com = new SqlCommand(sql,con);
con.Open();
for (; i < sql.Length; i++)
{
if (sql.Substring(i, 0) != " ")
break;
}
if(sql.Substring(i,6)=="select")
{
SqlDataReader dr;
dr=com.ExecuteReader();
while(dr.Read())
{
System.Console.WriteLine(dr[0].ToString());
}
}
else
{
com.ExecuteNonQuery();
System.Console.WriteLine("操作成功!");
}
System.Console.ReadLine();
}
}
class baseclass
{
public string sql(string filepath)
{
StreamReader str = null;
string sm="";
try
{
str = File.OpenText(filepath);
while (str.Peek() != -1)
{
sm = str.ReadLine();
}
str.Close();
}
catch
{
sm = "";
}
return sm;
}
public void resoust(string sql)
{

}

}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-08-28
Process.Start("cmd.exe ...");
剩下的就看你批处理的功底了
第2个回答  2008-08-28
本来想说调用ADO.NET
不过既然你另辟蹊径,那封装一个操作cmd的类就可以了
留个地址可以发给你

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网