如何从asp.net运行.sql文件 [英] How to run .sql file from asp.net
问题描述
我有一个.sql文件(脚本),其中包含mysql命令(如insert).
如何从asp.net运行此.sql文件?
例如:state.sql
插入`state1`
(`state_id`,`state_name`,`dstatus`)
值
(1,''aa'',0),
(2,''bb'',1),
(3,''cc'',0),
(4,''dd'',0),
(5,''ee'',1),
(6,''ff'',0),
(7,NULL,0),
(8,''dd'',NULL),
(9,NULL,NULL),
(10,''ff'',NULL),
(11,NULL,NULL);
我的文件包含数千条记录...
谁能帮忙..
I have a .sql file (Script) containing mysql command like insert.
How can i run this .sql file from asp.net??
For ex: state.sql
INSERT INTO `state1`
(`state_id`,`state_name`,`dstatus`)
VALUES
(1,''aa'',0),
(2,''bb'',1),
(3,''cc'',0),
(4,''dd'',0),
(5,''ee'',1),
(6,''ff'',0),
(7,NULL,0),
(8,''dd'',NULL),
(9,NULL,NULL),
(10,''ff'',NULL),
(11,NULL,NULL);
My file contains thousands of records...
Can any one help..
推荐答案
您需要指定文件名,然后首先读取字符串中的内容,然后执行CommandText命令.
PS:只要您的sql脚本中没有GO语句,这将起作用.
从SQL Server 2005开始,可以使用smo library
来执行此操作,而不能像以前一样使用批处理命令和osql连接.这是操作方法:
You need to specify file name and then first read it''s content in a string and then execute CommandText command.
PS: this will work as long you don''t have GO statement in your sql script.
Since SQL Server 2005, one can usesmo library
to do this operation and not use batch command and osql connection as previously. Here is how to do it:
using System.Data.SqlClient;
using System.IO;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string sqlConnectionString = "Data Source=(local);
Initial Catalog=AdventureWorks;Integrated Security=True";
FileInfo file = new FileInfo("C:\\myscript.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
}
}
参考:
在C#中运行.sql脚本文件 [ C#:执行包含以下内容的批处理T-SQL脚本GO语句 [
Refer:
Run a .sql script files in C# [^]
C#: Executing batch T-SQL Scripts containing GO statements[^]
这篇关于如何从asp.net运行.sql文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!