WinFrom 如何执行指定路径的A.sql文件? C:\A.sql用C#代码如何执行这个路径的sql文件? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 System.Diagnostics.Process.Start(@"c:\A.sql"); 原理:读取SQL文件中的文本,然后执行.以下代码供你参考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) { } }} Path.GetDirectoryName :返回指定路径字符串的目录信息; Path.GetExtension : 返回指定的路径字符串的扩展名; Path.GetFileName : 返回指定路径字符串的文件名和扩展名; Path.GetFileNameWithoutExtension :返回不具有扩展名的路径字符串的文件名; Path.GetPathRoot :获取指定路径的根目录信息;试试看。。 我编写的邮件发送程序,连续发送时主界面像死机一样 经过扫描仪生成的图片有哪些特定的属性?怎么得到它! GetString()可获取数据库中datatime类型数据吗 错误指示:使用了无效参数什么原因? VB转化为C# 大侠们,求正则表达式!! C# WORD 插入信息问题 C# N个数组相乘 ◆现在大家都使用什么测试管理软件?◆ c# 跳圈游戏...拜托大家帮忙有没有改进一下..随便什么语言...谢谢了... 求c# 网页标签 清除 正则表达式
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)
{
} }
}
Path.GetExtension : 返回指定的路径字符串的扩展名;
Path.GetFileName : 返回指定路径字符串的文件名和扩展名;
Path.GetFileNameWithoutExtension :返回不具有扩展名的路径字符串的文件名;
Path.GetPathRoot :获取指定路径的根目录信息;试试看。。