问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.OleDb;usingSystem.Data.SqlClient;usingSystem.Reflection;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceTestcreatesql{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){}privatevoidbutton1_Click(objectsender,EventArgse){if(t.Text==""){MessageBox.Show("请输入数据库名称","信息提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Exclamation);}else{DataSetds=newDataSet();//定义连接SQLserver数据库字符串stringconsqlserver="InitialCatalog=Test;DataSource=127.0.0.1,33066;UserID=sa;Password=123";SqlConnectioncon=newSqlConnection(consqlserver);try{con.Open();SqlCommandcmd=newSqlCommand();cmd.CommandText="CREATEDATABASE"+t.Text;cmd.Connection=con;cmd.ExecuteNonQuery();MessageBox.Show("数据库创建成功","信息提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information);}catch(Exceptionex){MessageBox.Show("数据库创建失败","信息提示",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);}finally{con.Close();con.Dispose();}}}}}