I’m new to .net and c# and want to connect Sql server express available in visual studio 2010.
I have searched a lot and read connection strings in connectionstrings.com but still don’t know how to connect.
i added a “service-based database” with adding new item and now have a db.mdf with tables.
my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication1
{
class First
{
static void Main()
{
SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=db2.mdf;Integrated Security=True");
con.Open();
Console.WriteLine("ok");
Console.ReadLine();
}
}
}
but it does not connect. in fact it executes with no errors , but no output for Console.WriteLine(“ok”);
how should be the connection-string in my case? specially for “Data source”. I don’t know what should its value be. please tell me the full connection-string which suits my case.
I’m really new to .net and confused many days for a simple connection.
also tried many other connection strings , but not working…
try with this code