PDA

View Full Version : How to manually add tables and values into SQL Server 2008


billy111
10-09-2009, 08:09 AM
Hey,

I am trying to add a table using SQL Server 2008, i have managed to do this, but can seem to work out how i can manually add rows into the database. I want to do a simple SELECT to ensure i can connect to the database so i need to enter some test information.

Also I have tried inserting something into the database, and this does not work, i am using the following code:-


protected void Button2_Click(object sender, EventArgs e)
{
string conString = "Data Source=tcp:esql2k801.discountasp.net;Initial Catalog=SQL2008_673062_kids;User ID=SQL2008_673062_kids_user;Password=********;";
SqlConnection empConnection = new SqlConnection(conString);

string insertStatement = "INSERT INTO tbl_users " + "([username], [password]) " + "VALUES ('Test', 'Test')";

SqlCommand insertCommand = new SqlCommand(insertStatement, empConnection);

empConnection.Open();

try
{
int count = insertCommand.ExecuteNonQuery();
}
catch (SqlException ex)
{

}
finally
{
empConnection.Close();
}
}


I think its the connection string thats is wrong, but need your adivce to help me out..

Regards

CrystalCMS
10-09-2009, 08:54 AM
The code looks OK but if it's failing, it could be an incorrect connection string or maybe a few other things:
1) The target db does not exist
2) The target db does exist but the [tbl_users] table does not
3) The values you're attempting to insert are of the wrong data type for the target table columns.

If you can post the exception thrown, perhaps it will be a bit clearer.

Joseph Jun
10-09-2009, 08:55 AM
What error message is being thrown?