我必须创建一个数据库,然后在创建的数据库内创建一个表。创建数据库和表
我已经连接到我的连接字符串主,但我想添加连接,并创建表时我刚才初始目录中创建的新的数据库名称。
这是我
string conn = "SERVER=BRIAN-PC\\SQLEXPRESS; Initial Catalog=master; user id =sa; Password=kagiso";
String str = "CREATE DATABASE IF NOT EXIST PSAHoldings ON PRIMARY"
+ "(NAME = PSAHoldings_Data,"
+ "FILENAME = 'C:\\PSAHoldings.mdf'',"
+ "SIZE = 2MB, FILEGROWTH =10%)"
+ "LOG ON (NAME = PSAHoldings_Log,"
+ "FILENAME = 'C:\\PSAHoldingsLog,idf',"
+ "SIZE = 1MB,"
+ "FILEGROTH = 10%)";
SqlConnection connection = new SqlConnection(conn);
connection.Open();
try
{
//SqlCommand to create database
SqlCommand cmd = new SqlCommand(str, connection);
cmd.ExecuteNonQuery();
MessageBox.Show("DataBase was successfully created", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
string connect = "Data Source=Esther-PC\\SQLEXPRESS; Initial Catalog=PSAHoldings; user id =sa; Password=kagiso";
string table = "CREATE TABLE IF NOT EXIST t_original (" +
"empId varChar(10) NOT NULL PRIMARY KEY," +
"paycode varChar(10) NOT NULL," +
"amount int NOT NULL," +
")";
SqlConnection con = new SqlConnection(connect);
con.Open();
SqlCommand createTable = new SqlCommand(table, con);
createTable.ExecuteNonQuery();
}
}
catch (SqlException sqlEx)
{
MessageBox.Show(sqlEx.ToString(), "Exception Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
connection.Close();
con.Close();
}
当我跑我不断收到异常错误的应用程序。
的异常错误读取它说:System.Data.SqlClient.SqlException(0x80131904): Incorrect synax near the keyword 'IF'. expression of non-boolean type specified in a context where a condition is expected, near 'PSAHoldings', Unclosed quotation mark after the character string 'Size=1mb, filegrowth=10%)'.
粘贴例外这里... – MajkeloDev 2015-03-31 09:48:05
什么做的异常* *说? – 2015-03-31 09:48:13
@MarcGravell我在编辑代码时粘贴了它 – Esther 2015-03-31 10:03:49