class Program
{
static void Main(string[] args)
{
try
{
ShowConnectionProp();
//UseSqlCommand();
//UseSqlDataReader();
//UseDataAdapterRead();
}
catch(Exception ex)
{
Console.WriteLine("异常"+ex.Message);
}
Console.Read();
} static void ShowConnectionProp( )
{
//数据库连接字符串
string conStr = "Data Source=(Local);Initial Catalog=Northwind;Integrated Security=True";
//用连接字符串创建数据库连接对像
SqlConnection con = new SqlConnection(conStr);
//在连接之前打印连接对象各属性信息这里把链接字符串 conStr 放到SqlConnection con = new SqlConnection(conStr);
后面括号里是什么意思啊? 还有这句啥意思啊? 我是小白。 最好能说通俗点。 不然我看不懂哈。谢谢各位了
try
{ }
catch(Exception ex)
{
Console.WriteLine("异常"+ex.Message);
}
这是一个创建对象的语句,分两步,
先声明 SqlConnection con ,类似平时的int i,然后利用new来调用对象的构造方法,这个构造方法有一个参数,即链接数据库的字符串。try
{ }
catch(Exception ex)
{
Console.WriteLine("异常"+ex.Message);
}
会捕获try{}里面发生的异常,并进行相应的处理。
也可以先构造对象,再传入参数的:
SqlConnection con = new SqlConnection();
con.ConnecteString=conStr;
con.open;
try
{ }
catch(Exception ex)
{
Console.WriteLine("异常"+ex.Message);
}
是程序的出错捕捉。当你的程序产生异常时可以被你程序自己捕获,而不至于跳到操作系统而中断程序。是提高程序的健壮性。