SqlDataReader reader = cmd.ExecuteReader();这句话出现异常,怎么办啊?
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSy...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
class ADOText
{
public string TextDB()
{
//步骤一,连接数据库 需要创建连接对象
string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
conn.Open(); //打开数据库
//步骤二:创建执行对象及SQL语句
string sql = "select * from 2b";//要执行的SQL语句
SqlCommand cmd = new SqlCommand(sql, conn);
//步骤三:创建数据读取对象,循环读取数据
SqlDataReader reader = cmd.ExecuteReader();
string content="";
while (reader.Read())
{
content += reader["name"].ToString();
}
//步骤四:关闭数据库连接
conn.Close();
return content;
}
}
} 展开
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
class ADOText
{
public string TextDB()
{
//步骤一,连接数据库 需要创建连接对象
string connString = @"Data Source=.\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
conn.Open(); //打开数据库
//步骤二:创建执行对象及SQL语句
string sql = "select * from 2b";//要执行的SQL语句
SqlCommand cmd = new SqlCommand(sql, conn);
//步骤三:创建数据读取对象,循环读取数据
SqlDataReader reader = cmd.ExecuteReader();
string content="";
while (reader.Read())
{
content += reader["name"].ToString();
}
//步骤四:关闭数据库连接
conn.Close();
return content;
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |