小编mjd*_*166的帖子

c# Winforms中不存在时如何创建数据库

如果数据库不存在,我想创建一个数据库。我正在尝试使用此代码执行此操作,但出现错误并收到此消息

在此处输入图片说明

请帮忙。

代码:

if(dbex == false)
{
    string str;

    SqlConnection mycon = new SqlConnection("Server=.\\sqlexpress;initial catalog=Masalehforoshi;Integrated security=SSPI;database=master");
    str = "CREATE DATABASE [Masalehforoshi] CONTAINMENT = NONE ON PRIMARY" +
                "(NAME=N'Masalehforoshi'," +
                @"FILENAME=N'C:\data\Masalehforoshi.mdf' " +
                ",SIZE=3072KB,MAXSIZE=UNLIMITED,FILEGROWTH=1024KB)" +
                "LOG ON (NAME=N'Masalehforoshi_log.', " +
                @"FILENAME=N'C:\Masalehforoshi_log.ldf' "+
                ",SIZE=1024KB,MAXSIZE=2048GB,FILEGROWTH=10%)";

    SqlCommand mycommand = new SqlCommand(str, mycon);

    try
    {
        mycommand.Connection.Open();
        mycommand.ExecuteNonQuery();
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.ToString(), "myprogram", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    finally
    {
        if(mycon.State == ConnectionState.Open)
        {
            mycon.Close();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# sql-server winforms

3
推荐指数
1
解决办法
8276
查看次数

标签 统计

c# ×1

sql-server ×1

winforms ×1