Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过脚本创建表&;如何运行脚本_C#_Sql_Rapidsql - Fatal编程技术网

C# 通过脚本创建表&;如何运行脚本

C# 通过脚本创建表&;如何运行脚本,c#,sql,rapidsql,C#,Sql,Rapidsql,我有几个脚本需要在我的数据库上运行。所以我有几个问题 在打开数据库连接时,我只需使用Connection、CommandText、CommandType和CommandTimeout 第一个问题-是否有人知道通过这种方法,我可以创建永久表,而不是临时表 第二,我将如何运行此文件?我是否可以将该文件设置为一个参数,然后在查询中运行该参数 谢谢用C写的#-- 您可以通过这种方式创建永久表和临时表 将脚本作为命令对象的CommandText运行 您可以在.NET SQL连接中执行SQL脚本中可以执行的

我有几个脚本需要在我的数据库上运行。所以我有几个问题

在打开数据库连接时,我只需使用Connection、CommandText、CommandType和CommandTimeout

第一个问题-是否有人知道通过这种方法,我可以创建永久表,而不是临时表

第二,我将如何运行此文件?我是否可以将该文件设置为一个参数,然后在查询中运行该参数

谢谢用C写的#--

  • 您可以通过这种方式创建永久表和临时表

  • 将脚本作为命令对象的CommandText运行


  • 您可以在.NET SQL连接中执行SQL脚本中可以执行的任何操作。至于“运行文件”,您需要将文件文本加载到内存中,并作为单个命令执行加载的文本


    我们在应用程序中执行类似的操作。我们的数据库脚本存储在SQL脚本中。我们按顺序将每个文件从磁盘加载到内存中并执行它。

    来自MSDN的示例:如何在查询中设置参数

    private static void UpdateDemographics(Int32 customerID,
        string demoXml, string connectionString)
    {
    
        string commandText = "UPDATE Sales.Store SET Demographics = @demographics "
            + "WHERE CustomerID = @ID;";
    
        using (SqlConnection connection = new SqlConnection(connectionString))
        {
            SqlCommand command = new SqlCommand(commandText, connection);
            command.Parameters.Add("@ID", SqlDbType.Int);
            command.Parameters["@ID"].Value = customerID;
    
            command.Parameters.AddWithValue("@demographics", demoXml);
    
            try
            {
                connection.Open();
                Int32 rowsAffected = command.ExecuteNonQuery();
                Console.WriteLine("RowsAffected: {0}", rowsAffected);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
    

    感谢您的回复,假设我需要不断更新该文件,因此我将该文件作为链接而不是实际文件,我是通过参数运行它,还是有其他方法?我如何将文本加载到内存中,例如使用(StreamReader sr-New StreamReader(“fileName”)。或者使用File.ReadAllLines()。完全读入文件,然后执行文本。为了更好地使用其他应用程序,请使用FileAccess.read和FileShare.read打开文件流,然后在文件流上打开StreamReader。