Anita Mathew

Anita Mathew

  • NA
  • 12
  • 3.4k

c# windows appliaction & database

Jun 23 2015 3:38 AM
I am making a  windows application that will check if a database exists on the local server? if it does'nt then create it through the program ..
 
here is the part of the program :
 
private void checkdbexists_Click(object sender, EventArgs e)
{
string sqlCreateDBQuery;
bool result = false;
SqlConnection tmpcon =new SqlConnection("Server= ...-PC;Integrated security= True; Initial Catalog= ....");
try
{
sqlCreateDBQuery = string.Format(" select databaseID from sys.databases where Name
= '{0}'", databasename);
using (tmpcon)
{
using (SqlCommand cmd = new SqlCommand(sqlCreateDBQuery, tmpcon))
{
tmpcon.Open();
object resultObj = cmd.ExecuteScalar();
int databaseID = 0;
if (resultObj != null)
{
int.TryParse(resultObj.ToString(), out databaseID);
}
tmpcon.Close();
result = (databaseID > 0);
label4.Text= " Database already exists";
}
}
}
catch (Exception ex)
{
result = false;
label4.Text = " Database does not exist";
}
return result;
}
but it gives me the following errors :
1.  Newline in constant for :  sqlCreateDBQuery = string.Format(" select databaseID from sys.databases where Name
= '{0}'", UPSC );
 
2.return keyword must not be followed by an object expression for:  return result;
 
 
need help to get the code running.. 

Answers (8)