求助:判断是否存在数据库
源代码:主要是判断是否存在数据库mobile
private void timer1_Tick(object sender, EventArgs e)
{
string strconnx = "User ID=sa;Password=123;Initial Catalog=master;Data Source=ycbbg";
string sqlx = "SELECT name FROM sys.databases WHERE name = N'mobile'";
int i = Convert.ToInt32(BaseClass.parameter_sql.ExecuteScalar(strconnx, CommandType.Text, sqlx,null));
if (i > 1)
{
frmLogin lo = new frmLogin();
lo.Show();
}
else
{
try
{
Directory.CreateDirectory(Application.StartupPath + "\\DataBase");
string sql2 = "CREATE DATABASE [mobile] ON PRIMARY ( NAME = N'mobile', FILENAME = N'" + Application.StartupPath + "\\DataBase\\mobile.mdf' , SIZE = 6144KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ) ( NAME = N'mobile_log', FILENAME = N'" + Application.StartupPath + "\\DataBase\\mobile_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%) CREATE TABLE [dbo].[tb_telsend]([id] [int] IDENTITY(1,1) NOT NULL,[telnum] [varchar](50) NULL,[telcontent] [varchar](max) NULL,[teltime] [varchar](50) NULL,CONSTRAINT [PK_tb_telsend] PRIMARY KEY CLUSTERED([id] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY] CREATE TABLE [dbo].tb_tel]([id] [int] IDENTITY(1,1) NOT NULL,[username] [varchar](50) NULL,[usersex] [varchar](10) NULL,[usertel] [varchar](50) NULL, CONSTRAINT [PK_tb_tel] PRIMARY KEY CLUSTERED([id] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY] CREATE TABLE [dbo].[worker]([id] [int] IDENTITY(1,1) NOT NULL,[jh] [varchar](50) NULL,shfzh][varchar](50) NULL,[danwei] [varchar](50) NULL,[xm] [varchar](50) NULL,[sex] [varchar](2) NULL,[address] [varchar](200) NULL,[phone] [varchar](50) NULL,[dnt]datetime] NULL,) ON [PRIMARY] CREATE TABLE [dbo].[times]([id] [int] IDENTITY(1,1) NOT NULL,[lx] [varchar](50) NULL,[days] [varchar](50) NULL,[times] [varchar](50) NULL,STRAINT [PK_times] PRIMARY KEY CLUSTERED([id] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY] CREATE TABLE [dbo].[tb_admin]([id] [int] IDENTITY(1,1) NOT NULL,[adminusername] [varchar](50) NULL,[adminuserpwd] [varchar](50) NULL,[power] [nchar](1) NULL,CONSTRAINT [PK_tb_admin] PRIMARY KEY CLUSTERED([id] ASC)WITH(IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY] CREATE TABLE [dbo].[tb_note]([id] [int] IDENTITY(1,1) NOT NULL,[type] [varchar](50) NULL,[note] [varchar](max) NULL,CONSTRAINT [PK_tb_note] PRIMARY KEY CLUSTERED([id] ASC)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY] CREATE TABLE [dbo].[tb_resvice]([id] [int] IDENTITY(1,1) NOT NULL,[smsnum] [varchar](50) NULL,[smscontent] [varchar](max) NULL,CONSTRAINT [PK_tb_resvice] PRIMARY KEY CLUSTERED([id] ASC)WITH (IGNORE_DUP_KEY = OFF) ON[PRIMARY]) ON [PRIMARY]";
BaseClass.parameter_sql.ExecuteNonQuery(strconnx, CommandType.Text, sql2, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
出错:int i = Convert.ToInt32(BaseClass.parameter_sql.ExecuteScalar(strconnx, CommandType.Text, sqlx,null));
输入字符串的格式不正确。