SqlServer数据库连接不上!!da.Fill(ds1)报“Fill: SelectCommand.Connection 属性尚未初始化”异常...
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = "Data Source = Sofia; uid = sa; pwd =a317066874; database = renvecf";//建立连接
cnn.Open();//打开连接
SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpter
SqlCommand cmd1 = new SqlCommand("select * from login , cnn");//sql语句
da.SelectCommand = cmd1;//设置为已实例化SqlDataAdapter的查询命令
DataSet ds1 = new DataSet();//实例化dataset
da.Fill(ds1);//把数据填充到dataset
dataGridView1.DataSource = ds1.Tables[0].DefaultView;//将数据集绑定datagridview,完成显示
}
}
} 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = "Data Source = Sofia; uid = sa; pwd =a317066874; database = renvecf";//建立连接
cnn.Open();//打开连接
SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpter
SqlCommand cmd1 = new SqlCommand("select * from login , cnn");//sql语句
da.SelectCommand = cmd1;//设置为已实例化SqlDataAdapter的查询命令
DataSet ds1 = new DataSet();//实例化dataset
da.Fill(ds1);//把数据填充到dataset
dataGridView1.DataSource = ds1.Tables[0].DefaultView;//将数据集绑定datagridview,完成显示
}
}
} 展开
2个回答
展开全部
这里是不是有问题?
SqlCommand cmd1 = new SqlCommand("select * from login , cnn");//sql语句
改成下面这个试试
SqlCommand cmd1 = new SqlCommand("select * from login", cnn);//sql语句
追问
da.Fill(ds1) 报错 的。。。
追答
你仔细看看你的代码吧,SqlCommand 和SqlConnection那点有关系 了,不报错才怪
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询