执行顺序或 ExecuteScalar 问题
首先,我将一个新成员插入成员表中。然后我查询表以获取会员 ID。我将数据存入表中,但它出现的速度不够快,无法在以下几行中执行查询。
我收到此异常“ExecuteScalar 需要一个打开且可用的连接。连接的当前状态已关闭。”我不明白这里出了什么问题。
//This code works fine
//Insert new members data
InsertMembers insert = new InsertMembers();
int age = Int32.Parse(txtAge.Text);
insert.InsertNewMember(txtEmail.Text, Myguid, txtName.Text, txtCity.Text, txtState.Text, txtDescription.Text, age, gender);
//This is the block thats failing
//Get Member Id to Insert into Pictures table
GetMemberInfo GetID = new GetMemberInfo();
int UMemberId = GetID.GetMemberId(Myguid);
Displayme.Text = UMemberId.ToString();
public int GetMemberID(string guid)
{
string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];
string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)";
int memberId;
using (var connection = new SqlConnection(strConectionString))
using (var command = new SqlCommand(StrSql, connection))
{
command.Parameters.Add("@GuidID", SqlDbType.VarChar).Value = guid;
memberId = (int)command.ExecuteScalar();
}
//returns 0 when it should be member id number
return memberId;
}
First im inserting a new member into the members table. Then im querying the table to get back the Member id. I get the data into the table, but it does not apear there quick enough to do the query in the following lines.
I get this exception "ExecuteScalar requires an open and available Connection. The connection's current state is closed." I cant figure out whats wrong here.
//This code works fine
//Insert new members data
InsertMembers insert = new InsertMembers();
int age = Int32.Parse(txtAge.Text);
insert.InsertNewMember(txtEmail.Text, Myguid, txtName.Text, txtCity.Text, txtState.Text, txtDescription.Text, age, gender);
//This is the block thats failing
//Get Member Id to Insert into Pictures table
GetMemberInfo GetID = new GetMemberInfo();
int UMemberId = GetID.GetMemberId(Myguid);
Displayme.Text = UMemberId.ToString();
public int GetMemberID(string guid)
{
string strConectionString = ConfigurationManager.AppSettings["DataBaseConnection"];
string StrSql = "SELECT MemberID FROM MEMBERS WHERE (Guid = @GuidID)";
int memberId;
using (var connection = new SqlConnection(strConectionString))
using (var command = new SqlCommand(StrSql, connection))
{
command.Parameters.Add("@GuidID", SqlDbType.VarChar).Value = guid;
memberId = (int)command.ExecuteScalar();
}
//returns 0 when it should be member id number
return memberId;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在执行命令之前,您应该调用
connection.Open()
:You should call
connection.Open()
, before executing the command:请仔细阅读错误消息。它与 ExecuteScalar 太快无关,也与操作顺序无关,除非有一个特定的操作缺失。 您尚未打开连接。
在
之前的
调用,您应该会体验到不同的结果。using
块范围内添加connection.Open();
ExecuteScalarRead the error message very carefully. It has nothing to do with ExecuteScalar being too quick, nor does it have to do with order of operations, except there is specifically an operation missing. You have not opened the connection.
Toss in a
connection.Open();
within the scope of theusing
blocks prior to theExecuteScalar
invocation and you should experience a different outcome.将这些代码行替换
为这些
using 语句用于自动处理连接,当您已经在 SqlConnection 上应用它时,我认为这里不需要应用 using with sql 命令。并且您错过了在执行命令之前打开连接。
Replace your these lines of code
with these
using statement is used to dispose the connection automatically and i don't think here is need to apply using with sql command when you have already applied it on SqlConnection. And you have missed to open the connection before executing the command.