创建数据库名称时出错

时间:2014-06-25 09:00:03

标签: c# wpf

我正在尝试在这里创建一个数据库,我只能用一个单词来创建它,比如“test”或“example”。但是,当我尝试创建一个数据库名称,中间有两个单词,例如“测试表”时,它不会创建数据库。我试过[],''和{}但它没有用。这是我的代码。

str = "CREATE DATABASE "+textBox1.Text+" ON PRIMARY " +
    "(NAME = "+textBox1.Text+"_Data, " +
    "FILENAME = 'C:\\"+textBox1.Text+".mdf', " +
    "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
    "LOG ON (NAME = "+textBox1.Text"+_Log, " +
    "FILENAME = 'C:\\"+textBox1.Text"+.ldf', " +
    "SIZE = 1MB, " +
    "MAXSIZE = 5MB, " +
    "FILEGROWTH = 10%)";

1 个答案:

答案 0 :(得分:0)

在本文Create a SQL Server database using C#中,您可以尝试sanitizing您的输入,例如:

    private void button2_Click(object sender, System.EventArgs e)
    {

        DatabaseParam DBParam = new DatabaseParam();
        DBParam.DatabaseName = textBox1.Text;

        //Assign Data file parameters
        DBParam.DataFileGrowth = "10%";
        DBParam.DataFileName = textBox3.Text;
        DBParam.DataFileSize = "2";//2MB at the init state
        DBParam.DataPathName = textBox2.Text;
        //Assign Log file parameters
        DBParam.LogFileGrowth = "10%";
        DBParam.LogFileName = textBox10.Text;
        DBParam.LogFileSize = "1";//1MB at the init state
        DBParam.LogPathName = textBox11.Text;


        CreateDatabase(DBParam);
    }

    private void CreateDatabase(DatabaseParam DBParam)
    {
        System.Data.SqlClient.SqlConnection tmpConn;            
        string sqlCreateDBQuery;
        tmpConn = new SqlConnection();
        tmpConn.ConnectionString = "SERVER = " + DBParam.ServerName + "; DATABASE = master; User ID = sa; Pwd = sa";                                                

        sqlCreateDBQuery =    " CREATE DATABASE " + DBParam.DatabaseName + " ON PRIMARY "
                            + " (NAME = " + DBParam.DataFileName +", " 
                            + " FILENAME = '" + DBParam.DataPathName +"', "
                            + " SIZE = 2MB,"
                            + " FILEGROWTH =" + DBParam.DataFileGrowth  +") "
                            + " LOG ON (NAME =" + DBParam.LogFileName +", "
                            + " FILENAME = '" + DBParam.LogPathName + "', "
                            + " SIZE = 1MB, "                               
                            + " FILEGROWTH =" + DBParam.LogFileGrowth  +") ";   

        SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn);           
        try
        {
            tmpConn.Open();
            MessageBox.Show(sqlCreateDBQuery);
            myCommand.ExecuteNonQuery();                

            MessageBox.Show("Database has been created successfully!", "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(ex.ToString(), "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information);                
        }
        finally
        {   
            tmpConn.Close();                
        }           
        return;     
    }

    private void textBox1_TextChanged(object sender, System.EventArgs e)
    {
        textBox3.Text = textBox1.Text + "_Data";
        textBox10.Text = textBox1.Text + "_Log";            
    }