本文介绍了如何从c#运行sql脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
i尝试从c#windows窗体运行sql脚本并发生此错误
Hi
i am try to run sql script from c# windows form and this error occurred
an exception occurred while executing a transact-sql statement or batch
这个代码
and this the code
string sqlConnectionString = BuildeConnectionString();
FileInfo file = new FileInfo(Application.StartupPath + @"\DBScript\Database_generate.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script );
file.OpenText().Close();
并且此行发生错误
server.ConnectionContext.ExecuteNonQuery(script);
and the error occurred in this line
server.ConnectionContext.ExecuteNonQuery(script );
推荐答案
cmd.Connection = conn;
cmd.CommandText = script
cmd.CommandTimeout = 15;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery()
这篇关于如何从c#运行sql脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!