检查数据库中是否存在两个表

时间:2015-12-02 19:20:38

标签: c# ms-access if-statement

我有数据库pavadinimas.mdf,它包含两个表:Vehicle和Repairs。我想检查数据库中是否存在两个表。所以,到目前为止,我设法检查是否存在一个表,但如何检查是否存在,如果不存在则创建它们。

这是我的代码:

string table1 = "Repairs";
                 SqlConnection conn;
                 conn = new SqlConnection(connection);
                     conn.Open();
                     string createString = "CREATE TABLE [dbo].['" + table1 + "'](" + "[VIN] [nvarchar](50)," + "[Taisymas] [nvarchar](50)," + "[Kaina] [decimal](18, 2))";
                       SqlCommand sqlCmd = new SqlCommand(createString, conn);
                       sqlCmd.ExecuteNonQuery();
                       conn.Close();

我也有应该创建表的代码。这是代码:

{{1}}

但是这段代码不会在我的数据库中创建表。然后我调用这个方法,它说该表已经存在,但是当我检查数据库中的表时,它什么都没有,空......

3 个答案:

答案 0 :(得分:3)

您正在寻找类似的东西:

IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='tbl1') AND EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='tbl2') SELECT 1 ELSE SELECT 0

答案 1 :(得分:1)

将其包裹在for循环

for(int i = 0; i < 2; i++){
  if (i = 0)
  {
    string tblnm = "Vehicle";
  }
  else
  {
    string tblnm = "Repairs";
  }

        SqlConnection conn;
        using (conn = new SqlConnection(connection))
        {
            conn.Open();           
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandText = @"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES 
                   WHERE TABLE_NAME='" + tblnm + "') SELECT 1 ELSE SELECT 0"; ;
            cmd.Connection = conn;
            cmd.ExecuteNonQuery();
            int x = Convert.ToInt32(cmd.ExecuteScalar());
            conn.Close();
            if (x == 2)
            {
                MessageBox.Show("Lentelės yra");
            }
            else
            {
                MessageBox.Show("Lenteliu nėra.Sukuriama");
            }
}

答案 2 :(得分:1)

如何使用参数并循环遍历表?

conn.Open();
var cmd = new System.Data.SqlClient.SqlCommand(
    @"SELECT count (*) FROM INFORMATION_SCHEMA.TABLES where TABLE_NAME = @TABLE_NAME",
    conn);
cmd.Parameters.Add("@TABLE_NAME", SqlDbType.VarChar);

List<String> tables = new List<string>() { "Vehicles", "Repairs" };

foreach (string tableName in tables)
{
    cmd.Parameters[0].Value = tableName;
    int x = Convert.ToInt32(cmd.ExecuteScalar());

    if (x == 0)
        CreateTable(tableName, conn);
}
conn.Close();

- 编辑 -

上面添加了

CreateTable方法,代码看起来像这样。警告 - 这是非常蛮力,但在没有其他信息的情况下,是完成任务的一种方法,因为我最了解你的问题。

private void CreateTable(String TableName, System.Data.SqlClient.SqlConnection conn)
{
    StringBuilder sql = new StringBuilder(@"create table [");
    sql.Append(TableName);
    sql.AppendLine(@"] (");

    switch (TableName)
    {
        case "Vehicle":
            sql.AppendLine("[VIN] varchar(100),");
            sql.AppendLine("[Manufacturer] varchar(100),");
            sql.AppendLine("[Model] varchar(100),");
            sql.AppendLine("[Year] integer");
            break;
        case "Repair":
            sql.AppendLine("[VIN] varchar(100),");
            sql.AppendLine("[Correction] varchar(100),");
            sql.AppendLine("[Price] decimal");
            break;
    }

    sql.Append(")");

    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(
        sql.ToString, conn);

    try
    {
        cmd.ExecuteNonQuery();
        MessageBox.Show("Created Table " + TableName);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Oops, I did it again");
    }
}