using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Reflection;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Testcreatesql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (t.Text == "")
{
MessageBox.Show("请输入数据库名称", "信息提示", System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation);
}
else
{
DataSet ds = new DataSet();
//定义连接SQLserver数据库字符串
string consqlserver = "Initial Catalog=Test;Data Source=127.0.0.1,33066;User ID=sa;Password=123";
SqlConnection con = new SqlConnection(consqlserver);
try
{
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "CREATE DATABASE" + t.Text;
cmd.Connection = con;
cmd.ExecuteNonQuery();
MessageBox.Show("数据库创建成功", "信息提示",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("数据库创建失败", "信息提示",
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error);
}
finally
{
con.Close();
con.Dispose();
}
}
}
}
}
create database后面少了个空格啊兄弟
果然,我是新手不太会,希望找个好友可以以后互相讨论
你把你的CommandText打出来看,就知道错在哪儿了!