简体   繁体   English

通过脚本创建表以及如何运行脚本

[英]Creating tables through a script & how to run the script

Ive got a couple of scripts that I need to run on my databse. 我有几个需要在数据库上运行的脚本。 So I have a few questions. 所以我有几个问题。

Im simply using Connection, CommandText,CommandType and CommandTimeout when opening a connection to the databse. 我在打开与数据库的连接时仅使用Connection,CommandText,CommandType和CommandTimeout。

First question - Does anyone know if through this method, I can create permantent tables, not temporary tables? 第一个问题-有人知道是否可以通过这种方法创建永久表,而不是临时表?

Secondly - How would I run this file? 其次-我将如何运行该文件? Could I just set the file to be a parameter, and in the query run the parameter? 我可以将文件设置为参数,然后在查询中运行参数吗?

Thanks 谢谢

In C#-- 在C#中

  1. You can create both permanent and temporary tables this way. 您可以通过这种方式创建永久表和临时表。

  2. Run the script as the CommandText of a command object. 将脚本作为命令对象的CommandText运行。

You can do anything in a .NET SQL connection that you could do in a SQL script. 您可以在.NET SQL连接中执行与在SQL脚本中可以执行的任何操作。 As far as "running a file", you would need to load the file text into memory and execute the loaded text as a single command. 至于“运行文件”,您需要将文件文本加载到内存中,并以单个命令的形式执行加载的文本。

We do something similar in our application. 我们在应用程序中执行类似的操作。 Our database scripts are stored in SQL scripts. 我们的数据库脚本存储在SQL脚本中。 We load each file sequentially from disk into memory and execute it. 我们将每个文件从磁盘顺序加载到内存中并执行它。

Example From MSDN : How to set parameters in the Query, 来自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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM