日期:2014-05-17  浏览次数:20434 次

asp.net我用代码新建了一个数据库,接下来建表的代码应该放哪啊
detailsview输入新建的数据库的信息,里有个插入和取消,我点插入就在ItemInserted事件中新建一个数据库,接下来我想在这个数据库中新建表,但是怎么试也不成功,不知道问题出在哪了
C# code
public partial class ProManagement_NewProgram : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Get("Project_ID") != null)
        {
            DetailsView1.DefaultMode = DetailsViewMode.Edit;
        }
    }
    protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        string str1;
        str1 = (DetailsView1.FindControl("TextBox1") as System.Web.UI.WebControls.TextBox).Text.ToString();
        String str;
         SqlConnection myConn = new SqlConnection("Server=LIUKAI-THINK;Integrated security=SSPI;database=master");
        str = "CREATE DATABASE "+str1+" ON PRIMARY " +
        "(NAME = str1_Data, " +
        "FILENAME = 'D:\\"+str1+".mdf', " +
        "SIZE = 3MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
        "LOG ON (NAME = MyDatabase_Log, " +
        "FILENAME = 'D:\\"+str1+".ldf', " +
        "SIZE = 1MB, " +
        "MAXSIZE = 5MB, " +
        "FILEGROWTH = 10%)";
        SqlCommand myCommand = new SqlCommand(str, myConn);
            myConn.Open();
            myCommand.ExecuteNonQuery();
            if (myConn.State == ConnectionState.Open)
            {
                myConn.Close();
            }
        EndEditing();
    }

我新建表的代码是
C# code
SqlConnection myConn = new SqlConnection("Server=LIUKAI-THINK;Integrated security=SSPI;database=master");
string str2;
        str2 = "CREATE TABLE Project" +
            "Project_ID int," +
            "(Name nvarchar(256) NULL," +
            "Owner nvarchar(256) NULL," +
            "Description char(10) NULL" +
            "PathToDatabaseFiles nvarchar(256) NULL" +
            "AutoShrink int NULL" +
            "Epoch datetime NULL" +
            "State int NULL)";
SqlCommand myCommand1 = new SqlCommand(str2, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();


------解决方案--------------------
建表的sql写错了,应该是:

string str2 = "CREATE TABLE Project" +
"(Project_ID int," +
"Name nvarchar(256) NULL," +
"Owner nvarchar(256) NULL," +
"Description char(10) NULL," +
"PathToDatabaseFiles nvarchar(256) NULL," +
"AutoShrink int NULL," +
"Epoch datetime NULL," +
"State int NULL)";