先看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace test2
{
class Program
{
static void Main(string[] args)
{
string connStr = "Data Source=(local);Initial Catalog=weeksun;Integrated Security=true";
SqlConnection connection = new SqlConnection(connStr);
try
{
connection.Open();
Console.WriteLine("DFGHEUYEUE");
}
catch (SqlException ex)
{
Console.WriteLine("出现异常{0}", ex.Message);
}
finally
{
connection.Close();
}
}
}
}数据库weeksun是存在的,运行该程序怎么会出现异常,连接不到数据库呢??求解释。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace test2
{
class Program
{
static void Main(string[] args)
{
string connStr = "Data Source=(local);Initial Catalog=weeksun;Integrated Security=true";
SqlConnection connection = new SqlConnection(connStr);
try
{
connection.Open();
Console.WriteLine("DFGHEUYEUE");
}
catch (SqlException ex)
{
Console.WriteLine("出现异常{0}", ex.Message);
}
finally
{
connection.Close();
}
}
}
}数据库weeksun是存在的,运行该程序怎么会出现异常,连接不到数据库呢??求解释。
//ps:如果是05及一下版本的话,才可以不写
string connStr =@"Data Source=.\sqlexpress;Initial Catalog=weeksun;Integrated Security=true";
http://www.connectionstrings.com/sql-server-2005
2、打开VS2008,新建一个windows项目
3、在表单上新建一个按钮,双击进入按钮事件,拷贝你上边提供的程序(从static void Main(string[] args)之下开始)
4、在前边加上using System.Data.SqlClient;
5、运行没有反应,在源程序加了一句MessageBox.Show("dddd");就好了
-------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
{
string connStr = "Data Source=(local);Initial Catalog=weeksun;Integrated Security=true";
SqlConnection connection = new SqlConnection(connStr);
try
{
connection.Open();
Console.WriteLine("DFGHEUYEUE");
MessageBox.Show("dddd"); }
catch (SqlException ex)
{
Console.WriteLine("出现异常{0}", ex.Message);
}
finally
{
connection.Close();
}
} }
string connStr = "server=.;database=weeksun;uid=sa;pwd=";