create table命令文本mysql用户自定义c#.net [英] create table command text mysql user defined c#.net
本文介绍了create table命令文本mysql用户自定义c#.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对此代码有疑问。我尝试根据用户在c#.net框架中定义的数据库创建表。
I'm having problem regarding this code. I try to create table based on database that user defined in c#.net framework.
SchemaName schemaForm = new SchemaName();
frmMetCon form2 = new frmMetCon();
if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string connStr = "datasource=localhost;port=3306;username=root;password=root;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
MySqlCommand command = conn.CreateCommand();
conn.Open();
command.CommandText = "DROP DATABASE IF EXISTS " + schemaForm.getData();
command.ExecuteNonQuery();
command.CommandText = "CREATE DATABASE " + schemaForm.getData();
command.CommandText = "CREATE TABLE Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
command.ExecuteNonQuery();
this.Hide();
form2.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}
错误显示没有选择数据库。如果我这样创建
The error showing that no database selected. if I create like this
command.CommandText = "CREATE TABLE" +schemaForm.getData()+ ".Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
我的命令会出错。请帮助
I will get error on the command. Please help
推荐答案
我认为你的连接字符串有问题
检查以下 MySQL连接字符串
I think you have a problem with your connection string
check the following MySQL connection strings
在执行Create table命令之前,必须执行create database命令,比如
Before you execute the Create table command you must execute the create database command, Like
try
{
MySqlCommand command = conn.CreateCommand();
conn.Open();
command.CommandText = "DROP DATABASE IF EXISTS " + schemaForm.getData();
command.ExecuteNonQuery();
command.CommandText = "CREATE DATABASE " + schemaForm.getData();
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
command.ExecuteNonQuery();
this.Hide();
form2.Show();
}
SchemaName schemaForm = new SchemaName();
frmMetCon form2 = new frmMetCon();
if (schemaForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string connStr = "datasource=localhost;port=3306;username=root;password=root;";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
MySqlCommand command = conn.CreateCommand();
conn.Open();
command.CommandText = "DROP DATABASE IF EXISTS " + schemaForm.getData();
command.ExecuteNonQuery();
command.CommandText = "CREATE DATABASE " + schemaForm.getData();
command.ExecuteNonQuery();
command.CommandText = "USE " + schemaForm.getData();
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE Metabolites(MetaboliteID VARCHAR(10) NOT NULL, Metabolite_Name VARCHAR(45) NULL, ReactionTime INT NULL, PRIMARY KEY (MetaboliteID)";
command.ExecuteNonQuery();
this.Hide();
form2.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
conn.Close();
}
这篇关于create table命令文本mysql用户自定义c#.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文