好的,我有一些用于登录的 VB 代码,我希望它现在可以在 C# 中工作。我以为这会很直接,但我错了。行: string connString = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString; 在 ConnectionStrings 处引发错误,我不知道为什么。同样在 while 语句中,objDR 说它是一个变量,但被用作一种方法。任何帮助都会很棒。以下是整个代码:
Using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void
btnSubmit_Click(object sender, System.EventArgs e)
{
if (((string.IsNullOrEmpty(txtUserName.Text))))
{
lblErrorMessage.Text = "Username must be entered.";
txtUserName.Focus();
return;
}
string connString = ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString;
System.Data.SqlClient.SqlConnection myConnection = new System.Data.SqlClient.SqlConnection(connString);
string sql = "Select * From TCustomers";
System.Data.SqlClient.SqlDataReader objDR = default(System.Data.SqlClient.SqlDataReader);
System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand(sql, myConnection);
myConnection.Open();
objDR = objCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
bool blnLogin = false;
string strPassword = null;
string strUserName = null;
strPassword = txtPassword.Text;
strPassword = strPassword.Trim();
strUserName = txtUserName.Text;
strUserName = strUserName.Trim();
while (objDR.Read())
{
if (((objDR("strUserName").ToString().Trim() == strUserName)) & ((objDR("strPassword").ToString().Trim() == strPassword)))
{
blnLogin = true;
Session["CustomerID"] = objDR("intCustomerID");
Session["UserName"] = objDR("strUserName");
Session["FirstName"] = objDR("strFirstName");
Session["LastName"] = objDR("strLastName");
Session["Email"] = objDR("strEmailAddress");
Session["UserType"] = objDR("intUserTypeID");
break; // TODO: might not be correct. Was : Exit While
}
}
}
}