[英]dataReader reader = cmd.executereader() not working
I am stuck in simple datareader problem . 我陷入了简单的datareader问题。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
public void proName(string str)
{
SqlDataReader reader;
using (SqlConnection sqlcon = new SqlConnection(constr))
{
string proId = textBox3.Text;
SqlCommand sqlcmd = new SqlCommand("SELECT proName FROM products where proId = proId;", sqlcon);
sqlcmd.CommandType = CommandType.TableDirect;
reader = cmd.executereader();
}
}
and it showing error under cmd / command (the name cmd does not exit in current context) ..... 它在cmd /命令下显示错误(名称cmd在当前上下文中不存在)...
I have tried create object before and after connection but not working. 我尝试在连接之前和之后创建对象,但无法正常工作。
Command type shouldn't be TableDirect . 命令类型不应为TableDirect 。
sqlcmd.CommandType = CommandType.Text; // which is default
reader = sqlcmd.executereader();
The variable name is sqlcmd
and you're asking for cmd
on the executereader()
function. 变量名称为
sqlcmd
并且您需要在executereader()
函数上输入cmd
。
sqlcmd.CommandType = CommandType.TableDirect;
reader = cmd.executereader();
Should be 应该
sqlcmd.CommandType = CommandType.TableDirect;
reader = sqlcmd.executereader();
Also consider CharithJ's answer, as he stated: 还请考虑一下CharithJ的回答,正如他所说:
Command type shouldn't be TableDirect.
命令类型不应为TableDirect。
Below should work.. 下面应该工作..
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
public void proName(string str)
{
SqlDataReader reader;
using (SqlConnection sqlcon = new SqlConnection(constr))
{
string proId = textBox3.Text;
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("SELECT proName FROM products where proId = proId;", sqlcon);
sqlcmd.CommandType = CommandType.Text;
reader = sqlcmd.ExecuteReader();
if (reader.Read())
{
Response.Write(reader[0].ToString());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.