我在 SQL Server 上测试了查询,它可以工作,但不能在 C# 上工作。错误消息指向我的查询返回的变量的存储机制,但我认为问题可能是查询本身。
在 Delphi 中,有一种方法可以调试查询语句(showMessage
statement)。
例如:-
DMS.ADOQuery1.SQL.Clear;
DMS.ADOQuery1.SQL.Add('select FIRSTNM, LASTNM, STREET, STREET2, CITY, STATE, ZIP, MEMBERKEY, MEMBID, BIRTH' +
' from MEMBER' +
' where MEMBID = ''' + Mem_ID + ''''
);
showmessage(DMS.ADOQuery1.SQL[0]);
有没有办法在 C# 中做类似的事情?谁能看一下,让我知道为什么 SQL 查询在 ASP.net 中不起作用但在 SQL Server 中起作用?
// initialize
string [] HPCODE = new string[20] ;
string [] OPFROMDT = new string [20] ;
string [] OPTHRUDT = new string [20] ;
// Second SQL Query to find out eligibility information about the requested Member. //
SqlConnection Connection1 = new SqlConnection(DBConnect.SqlServerConnection);
String strSQL1 = "SELECT MEMBER, HPCODE, convert(varchar, OPFROMDT, 101) as OPFROMDT, convert(varchar, OPTHRUDT, 101) as OPTHRUDT FROM [main].[dbo].[MEMBER] INNER JOIN [main].[dbo].[MEMBHP] ON MEMBER.MEMBERKEY = MEMBHP.MEMBERKEY and opthrudt >= opfromdt INNER JOIN [main].[dbo].[HPCONTRACT] ON MEMBHP.HPCODEKEY = HPCONTRACT.HPCODEKEY INNER JOIN [main].[dbo].[LOB] ON HPCONTRACT.LOB_KEY = LOB.LOB_KEY where MembID = @memID";
SqlCommand command1 = new SqlCommand(strSQL1, Connection1);
//command1.Parameters.AddWithValue("@memID", memID);
// SQL reader variable is set.
SqlDataReader Dr1;
// Connection is explicitly opened.
Connection1.Open();
// Reader is executed.
Dr1 = command1.ExecuteReader();
int i = 0;
while (Dr1.Read())
{
// if (memID == Dr["MEMBID"].ToString() )
// {
// store subID in the Global variable called ID.
HPCODE[i] = (Dr1["HPCODE"].ToString()).TrimEnd();
OPFROMDT[i] = (Dr1["OPFROMDT"].ToString()).TrimEnd();
OPTHRUDT[i] = (Dr1["OPTHRUDT"].ToString()).TrimEnd();
i = i + 1;
// }
}
// Reader variable must always be explicitly closed to prevent memory leaks.
Dr1.Close();
错误:
用户代码未处理的索引超出范围异常:=HPCODE
注意:-
Parameters.AddwithValue
未注释,因为它是在我之前的代码部分中预定义的。如果解决方案这么简单,我会自己解决。*