J
Joesoft11a
Guest
After figuring out the issue with the connection string. I been able to get my code to create the database. How ever I can't seem to get it to create the tables. I updated the code to include some table info I left out. I posted the new code below.
From what I can see the error is on line 130. That is where this runs " command2.ExecuteNonQuery();"
public bool makeSQLdb()
{
bool result = false;
//conn.ConnectionString = "Data Source=(local)\SQLEXPRESS; Initial Catalog=master;Trusted_Connection = true;";
try
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS; Initial Catalog=master;Trusted_Connection = true;"))
{
conn.Open();
string cmddb = "SELECT * FROM master.dbo.sysdatabases where name = 'mlhelper'";
using (SqlCommand sqlCmd = new SqlCommand(cmddb, conn))
{
//Create the database 1st.
int nRet = sqlCmd.ExecuteNonQuery();
if (nRet <= 0)
{
using (SqlCommand command = new SqlCommand("CREATE DATABASE mlhelper", conn))
command.ExecuteNonQuery();
}
}
using (SqlCommand sqlCmd = new SqlCommand(cmddb, conn))
{
//int nRet = sqlCmd.ExecuteNonQuery();
conn.ChangeDatabase("mlhelper");
//Create the tables last.
using (SqlCommand command2 = new SqlCommand("CREATE TABLE bases(id int PRIMARY KEY not null AUTO_INCREMENT,basename char(50),category char(10);", conn))
{ command2.ExecuteNonQuery(); }
using (SqlCommand command3 = new SqlCommand("CREATE TABLE maps(id int PRIMARY KEY not null AUTO_INCREMENT,mapname char(50),description char(500),filename (13),filedate(8),time timestamp(8),baseid int,category char(50);", conn))
{ command3.ExecuteNonQuery(); }
using (SqlCommand command4 = new SqlCommand("CREATE TABLE category(id int PRIMARY KEY not null AUTO_INCREMENT,cname char(50);", conn))
{ command4.ExecuteNonQuery(); }
using (SqlCommand command5 = new SqlCommand("INSERT INTO TABLE category(id int PRIMARY KEY not null AUTO_INCREMENT,cname) VALUES(1 ,'jointops');", conn))
{ command5.ExecuteNonQuery(); }
result = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Creating Error :" + ex.Message + "" + ex.StackTrace.ToString(), Application.ProductName);
this.Close();
}
return result;
}
here's the error I'm getting in a screen shot.
anyone have any idea what I'm doing wrong.
http://www.df-barracks.com Delta Force Barracks
http://www.starfiresoft.com Starfire Software
Continue reading...
From what I can see the error is on line 130. That is where this runs " command2.ExecuteNonQuery();"
public bool makeSQLdb()
{
bool result = false;
//conn.ConnectionString = "Data Source=(local)\SQLEXPRESS; Initial Catalog=master;Trusted_Connection = true;";
try
{
using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS; Initial Catalog=master;Trusted_Connection = true;"))
{
conn.Open();
string cmddb = "SELECT * FROM master.dbo.sysdatabases where name = 'mlhelper'";
using (SqlCommand sqlCmd = new SqlCommand(cmddb, conn))
{
//Create the database 1st.
int nRet = sqlCmd.ExecuteNonQuery();
if (nRet <= 0)
{
using (SqlCommand command = new SqlCommand("CREATE DATABASE mlhelper", conn))
command.ExecuteNonQuery();
}
}
using (SqlCommand sqlCmd = new SqlCommand(cmddb, conn))
{
//int nRet = sqlCmd.ExecuteNonQuery();
conn.ChangeDatabase("mlhelper");
//Create the tables last.
using (SqlCommand command2 = new SqlCommand("CREATE TABLE bases(id int PRIMARY KEY not null AUTO_INCREMENT,basename char(50),category char(10);", conn))
{ command2.ExecuteNonQuery(); }
using (SqlCommand command3 = new SqlCommand("CREATE TABLE maps(id int PRIMARY KEY not null AUTO_INCREMENT,mapname char(50),description char(500),filename (13),filedate(8),time timestamp(8),baseid int,category char(50);", conn))
{ command3.ExecuteNonQuery(); }
using (SqlCommand command4 = new SqlCommand("CREATE TABLE category(id int PRIMARY KEY not null AUTO_INCREMENT,cname char(50);", conn))
{ command4.ExecuteNonQuery(); }
using (SqlCommand command5 = new SqlCommand("INSERT INTO TABLE category(id int PRIMARY KEY not null AUTO_INCREMENT,cname) VALUES(1 ,'jointops');", conn))
{ command5.ExecuteNonQuery(); }
result = true;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Creating Error :" + ex.Message + "" + ex.StackTrace.ToString(), Application.ProductName);
this.Close();
}
return result;
}
here's the error I'm getting in a screen shot.
anyone have any idea what I'm doing wrong.
http://www.df-barracks.com Delta Force Barracks
http://www.starfiresoft.com Starfire Software
Continue reading...