嗨,我有一個登錄連接到服務器字符串,並在登錄按鈕被按下時執行此操作。如果用戶和密碼未存儲在數據庫中,它將返回無效錯誤,但如果它們位於數據庫上,則會引發「索引位於數組邊界外」錯誤。我將如何解決這個問題? 謝謝索引在c#上的數組錯誤的邊界外登錄使用SQL
我的連接字符串位於我的appconfig文件中,問題可以在那裏嗎?
########## Windows表單登錄後按一下按鈕private void btnOK_Click(object sender, EventArgs e)
{
SqlConnection con = Program.GetConnection;
SqlDataReader dr = null;
try
{
SqlCommand cmd = new SqlCommand("SELECT * FROM Users WHERE UserName='" +
txtName.Text + "'AND Password='" + textpassword.Text + "'", con);
dr = cmd.ExecuteReader();
if (dr.Read())
{
Program.UserLoginName = dr.GetString(3);
this.Close();
}
else
MessageBox.Show("Invalid Username & Password!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
####### Program.cs的文件
Using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace FrontEndV1
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
public static SqlConnection GetConnection
{
get
{
string ConnectionString = ConfigurationManager.ConnectionStrings["FrontEndV1Connection"].ConnectionString;
SqlConnection con = new SqlConnection(ConnectionString);
con.Open();
return con;
}
}
public static string UserLoginName { get; set; }
}
}
-1未響應。 **用戶**表中有多少列? – adatapost
@AVD:我認爲在這種情況下,這是不適合downvote用戶,它被視爲「18分鐘前」相應的SO統計。我相信你可以在相同的情況發佈問題和sidetracked一段時間,這不是一個downvote的原因 – sll