例如用ExecuteNonQuery()方法执行create操作
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=PSDB;Integrated Security=SSPI");
string str = "CREATE TABLE aaa ( " +
"[ID] [int] IDENTITY (1, 1) NOT NULL , " +
"[BasicID] [int] NULL ," +
"[AdoptedName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ," +
"[AdoptedSex] [char] (2) COLLATE Chinese_PRC_CI_AS NULL ," +
"[AdoptBirthday] [smalldatetime] NULL ," +
"[AdoptedType] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL ," +
"[ApprTime] [smalldatetime] NULL ," +
"[Remark] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL " +
") ON [PRIMARY] ";
SqlCommand comm = new SqlCommand(str, conn);
int i = 10;
try
{
conn.Open();
i = comm.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Response.Write(i.ToString());
如果执行成功的话 返回的值为-1,如果数据表已经存在的话返回异常:数据库中已存在名为 'aaa' 的对象。