Im newbie in C#
I put my database connection to a class:
public class Connection
{
public string SetConnection()
{
string connectionstring = "server=SURI-PC;database=cms;Integrated Security=True";
return connectionstring;
}
}
then, I call it in main class:
public static void Main(string[] args)
{
Connection conObject = new Connection();
SqlConnection scon = new SqlConnection(conObject.SetConnection());
String sql = "insert into category(cat_id, cat_name) values('C03', 'Browser')";
SqlCommand cmd = new SqlCommand(sql, scon);
cmd.ExecuteNonQuery();
}
But it isnt work.
How to create a connection class and call it in others class?
Please help me!
thanks.
Yeah like zenwalker said, there is no scon.Open() which you need to open the connection to the database. Also it’s better practice to use a using-block when you open the connection like this
Also see here for more information about the SqlConnection.