我創建一個程序,其中,用戶將登錄到系統,但它得到這樣的錯誤,類型的未處理的異常「System.Data.OleDb.OleDbException」發生
而且我使用來自我的數據庫的c#編程語言和Ms Access。
這是我的整個代碼。
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Data.OleDb;
using System.Drawing.Drawing2D;
namespace Shakeys_Inventory_System
{
public partial class frmLogin : Form
{
private OleDbConnection cn = new OleDbConnection();
public frmLogin()
{
//SplashScreen
Thread t = new Thread(new ThreadStart(SplashStart));
t.Start();
Thread.Sleep(2500);
InitializeComponent();
this.BackColor = Color.White;
panel1.BackColor = Color.FromArgb(25, Color.Black);
t.Abort();
cn.ConnectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C: \Users\Raphael\Documents\Visual Studio 2015\Projects\Shakeys_Inventory_System\Shakeys_Inventory_System\ShakeysDatabase.accdb");
}
//Blurred Panel *START
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Yellow, 0, 0, 100, 100);
}
public void SplashStart()
{
Application.Run(new frmSplashScreen());
}
public void DrawPath(Pen pen, GraphicsPath path)
{
}
private void frmLogin_Load(object sender, EventArgs e)
{
}
//Button Login Start
private void btn_Login_Click(object sender, EventArgs e)
{
cn.Open();
OleDbCommand cm = new OleDbCommand();
cm.Connection = cn;
cm.CommandText = "Select * from UsernameInfo where Username = '" +txt_Username.Text+ "' and password = '"+txt_Password.Text+"'";
OleDbDataReader odr = cm.ExecuteReader();
int cnt = 0;
while (odr.Read())
{
cnt++;
}
if (cnt == 1)
{
this.Hide();
frmHomepage fhp = new frmHomepage("Welcome:"+ txt_Username.Text);
fhp.ShowDialog();
}
else
{
MessageBox.Show("Username or Password is Incorrect!", "ERROR!",MessageBoxButtons.OK,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
}
cn.Close();
}
//Button Quit *Start
private void btn_Quit_Click(object sender, EventArgs e)
{
DialogResult Result1 = MessageBox.Show("Do you want to CLOSE the program?", "Warning!",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (Result1 == DialogResult.Yes)
{
Application.Exit();
}
}
}
}
順便說一句,我創造了我自己的數據庫中MSACESS和路徑的文件複製並填寫Flash到我,
cn.ConnectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C: \Users\Raphael\Documents\Visual Studio 2015\Projects\Shakeys_Inventory_System\Shakeys_Inventory_System\ShakeysDatabase.accdb");
的路徑文件是正確的,我的查詢也是正確的,表還,我的問題是,爲什麼我得到一個錯誤?
感謝您的幫助!
哪行代碼會拋出錯誤? –
*爲什麼我得到一個錯誤?*,僅僅給出異常類型是不夠的。您需要提供異常消息,最好是堆棧跟蹤。 (拋出異常的內部異常也是如此)。 –
開放式的SQL注入。參數化您的查詢。 – DRapp