我确定这个问题已经得到解答,但我无法使用搜索工具找到答案.
使用c#我想运行一个.sql文件.sql文件包含多个sql语句,其中一些语句分为多行.我尝试在文件中读取并尝试使用ODP.NET执行文件...但是我不认为ExecuteNonQuery真的是为了这样做而设计的.
所以我尝试通过生成一个进程来使用sqlplus ...但是除非我将UseShellExecute设置为true而生成进程,否则sqlplus会挂起并永远不会退出.这是不起作用的代码.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "sqlplus";
p.StartInfo.Arguments = string.Format("xx/xx@{0} @{1}", in_database, s);
p.StartInfo.CreateNoWindow = true;
bool started = p.Start();
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
WaitForExit永远不会返回....除非我将UseShellExecute设置为true.UseShellExecute的副作用是您无法捕获重定向的输出.
我正在构建一个自定义数据库部署实用程序,我需要读取包含sql脚本的文本文件并对数据库执行它们.
非常简单的东西,到目前为止一切顺利.
但是我遇到了一个障碍,文件的内容被成功完整地读取,但是一旦传入SqlCommand然后用SqlCommand.ExecuteNonQuery执行,只执行部分脚本.
我启动了Profiler并确认我的代码没有传递所有脚本.
private void ExecuteScript(string cmd, SqlConnection sqlConn, SqlTransaction trans)
{
SqlCommand sqlCmd = new SqlCommand(cmd, sqlConn, trans);
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandTimeout = 9000000; // for testing
sqlCmd.ExecuteNonQuery();
}
// I call it like this, readDMLScript contains 543 lines of T-SQL
string readDMLScript = ReadFile(dmlFile);
ExecuteScript(readDMLScript, sqlConn, trans);
Run Code Online (Sandbox Code Playgroud) 我正在EF Code First中创建现有应用程序的重写,但我需要从现有应用程序的数据库中导入一些数据.这是我的实体类的定义:
public class Business : EntityBase
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AccountNumber { get; set; }
Run Code Online (Sandbox Code Playgroud)
现在,导入的记录需要保留AccountNumber旧系统的价值.但是,新值应使用IDENTITYDB生成的值.
如何IDENTITY在导入旧记录时关闭,然后在应用程序的剩余生命周期内重新打开它?在导入记录之前执行此操作无效:
context.Database.ExecuteSqlCommand("set identity_insert Businesses on");
Run Code Online (Sandbox Code Playgroud)