2013-10-28
展开全部
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();
}
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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2017-07-09
展开全部
下面我做了一个,单击按钮后,查询数据库中数据的记录代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Data.SqlClient;//首先要导入数据库命名空间namespaceEXP6{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse)//单击按钮的事件{stringconnString="DataSource=.;InitialCatalog=stuDB;IntegratedSecurity=True";//连接数据字符串,SqlConnectionconnection=newSqlConnection(connString);//创建SqlConnection连接对象intnum=0;stringmessage="";stringsql="selectcount(*)fromstuInfo";//用于查询的sql语句try{connection.Open();//调用对象的Open()方法,打开数据库连接SqlCommandcommand=newSqlCommand(sql,connection);//用要执行的sql语句跟connection连接对象去创建一个SqlCommand对象num=(int)command.ExecuteScalar();//调用command对象的ExecuteScalar()方法,该方法可以返回单个值,我这里查的是count(*),返回的值是Object类型,把它强制转换成int,用一个num变量接受message=string.Format("stuInfo表中有{0}条学员信息!",num);MessageBox.Show(message,"查询结果",MessageBoxButtons.OK,MessageBoxIcon.Information);//输出消息}catch(Exception){MessageBox.Show("存在异常");//处理异常}finally{connection.Close();//显式关闭数据库连接。使用ADO数据操作,都注意要显式关闭MessageBox.Show("关闭数据库连接成功");}}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询