sql数据库连接问题
本人初学asp.net,sqlserver也没有用过。网上有人说vs2008里自带的sql2005只能用于调试,如果建立站点还需要安装sql2005.但我在网上下了好几个版本都装不上,安装过程中老是提示错误,我的机子是win7。
偶然的一谈,在vs里乱点,添加了一个数据连接。好像稀里糊涂的建了一个数据库节点,并添加了我需要的几张数据表。在asp页面上添加了gridview 通过配置数据源建立了连接,并把连接字符串存到了config文件中。
<connectionStrings> <!--数据库连接字符串--> <add name="forconnstr" connectionString="Data Source=AKSLJ\SQLEXPRESS;Initial Catalog=forcastserver;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/> </connectionStrings>
protected void Button1_Click(object sender, EventArgs e) { DateTime MyDate = DateTime.Parse(TextBox1.Text); DateTime MyDate1 = DateTime.Parse(TextBox2.Text); //使用ADO连接数据库=AKSLJ\SQLEXPRESS //string SqlStr="Server=(local);user id=sa;Pwd=; DataBase=forcastserver"; //用这条语句连不上 string SqlStr = "Data Source=SQLEXPRESS;Initial Catalog=forcastserver;Integrated Security=True;Pooling=False"; SqlConnection con=new SqlConnection(SqlStr); con.Open(); //[color=#FF0000]这里出错!!!![/color] string sql= "select * from test where 日期>='"+MyDate +"'and 日期<='"+MyDate1 +"'"; SqlDataAdapter dap=new SqlDataAdapter(sql ,con); DataSet ds = new DataSet(); dap.Fill(ds); GridView2.DataSource = ds.Tables[0].DefaultView; con.Close(); }