C#怎样创建SQL数据库

  • Post author:
  • Post category:其他


1、建立连接

System.Data.SqlClient.SqlConnection   oConn=new   System.Data.SqlClient.SqlConnection(“data   source=”+this.DbServer.Text+”;initial   catalog=master;user   id=”+this.UserId.Text+”;password=”+this.Password.Text);

2、//建立数据库

System.Data.SqlClient.SqlCommand   oComm=oConn.CreateCommand();

oComm.CommandText=”CREATE   DATABASE   “+this.DBName.Text   ;

try

{

oComm.ExecuteNonQuery();

}

catch

{

System.Windows.Forms.MessageBox.Show(this,”建立数据库出错,请手工建立指定的数据库”,”信息提示”,System.Windows.Forms.MessageBoxButtons.OK);

oComm.Dispose();

3、转换到新建的数据库

oConn.ChangeDatabase(this.DBName.Text);

4、建立其他对象

oCommand.CommandText=”if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[AddAnalyzeRecord]’)   and   OBJECTPROPERTY(id,   N’IsProcedure’)   =   1)/n”;

oCommand.CommandText+=”drop   procedure   [dbo].[AddAnalyzeRecord]/n”;

oCommand.ExecuteNonQuery();

oCommand.CommandText=”/n”;

oCommand.CommandText+=”if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[AddVisitErrorLog]’)   and   OBJECTPROPERTY(id,   N’IsProcedure’)   =   1)/n”;

oCommand.CommandText+=”drop   procedure   [dbo].[AddVisitErrorLog]/n”;

oCommand.ExecuteNonQuery();

oCommand.CommandText=”/n”;

oCommand.CommandText+=”if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[AddVisitLog]’)   and   OBJECTPROPERTY(id,   N’IsProcedure’)   =   1)/n”;

oCommand.CommandText+=”drop   procedure   [dbo].[AddVisitLog]/n”;

oCommand.ExecuteNonQuery();

oCommand.CommandText=”/n”;

oCommand.CommandText+=”if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[AnalyzeRecord]’)   and   OBJECTPROPERTY(id,   N’IsUserTable’)   =   1)/n”;

oCommand.CommandText+=”drop   table   [dbo].[AnalyzeRecord]/n”;

oCommand.ExecuteNonQuery();

oConn.Close();

oConn.Dispose();

}



版权声明:本文为luochengbang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。