类型' System.Data.SqlClient.SqlException'的第一次机会异常发生在System.Data.dll 90210中

时间:2015-04-18 02:07:28

标签: c#

其他信息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。

找不到服务器或无法访问服务器。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。

  

(提供者:SQL网络接口,错误:26 - 错误定位   指定的服务器/实例)        da.SelectCommand = cmd;                           da.Fill(DS);

代码:

  public string LogNotification { get; set; }
            public bool ConfirmLogin(string id, string pw)
            {
                using (SqlConnection con = new SqlConnection("Data Source=C:\\RegistrationMDB.mdb"))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();

                    SqlCommand cmd = new SqlCommand("SELECT ID, PASSWORD FROM Students WHERE ID = @ID OR PASSWORD = @PASSWORD", con);
                    da.SelectCommand = cmd;
                    da.Fill(ds);
                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = id;
                    cmd.Parameters.Add("@PASSWORD", SqlDbType.VarChar).Value = pw;






                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        iD = (dr["@ID"].ToString());
                        password = dr["@PASSWORD"].ToString();
                    }

                    if (iD == id && password == pw)
                    {
                        return true;
                    }
                    else
                    {
                        LogNotification = "ID/Password is incorrect";
                        return false;
                    }
                }
            }

数据库代码;

 //++++++++++++++++  DATABASE Data Elements +++++++++++++++++
        public System.Data.OleDb.OleDbDataAdapter OleDbDataAdapter;
        public System.Data.OleDb.OleDbCommand OleDbSelectCommand;
        public System.Data.OleDb.OleDbCommand OleDbInsertCommand;
        public System.Data.OleDb.OleDbCommand OleDbUpdateCommand;
        public System.Data.OleDb.OleDbCommand OleDbDeleteCommand;
        public System.Data.OleDb.OleDbConnection OleDbConnection;
        public String cmd;

        public void DBSetup(){
        // +++++++++++++++++++++++++++  DBSetup function +++++++++++++++++++++++++++
        // This DBSetup() method instantiates all the DB objects needed to access a DB, 
        // including OleDbDataAdapter, which contains 4 other objects(OlsDbSelectCommand, 
        // oleDbInsertCommand, oleDbUpdateCommand, oleDbDeleteCommand.) And each
        // Command object contains a Connection object and an SQL string object.
            OleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter();
            OleDbSelectCommand = new System.Data.OleDb.OleDbCommand();
            OleDbInsertCommand = new System.Data.OleDb.OleDbCommand();
            OleDbUpdateCommand = new System.Data.OleDb.OleDbCommand();
            OleDbDeleteCommand = new System.Data.OleDb.OleDbCommand();
            OleDbConnection = new System.Data.OleDb.OleDbConnection();


            OleDbDataAdapter.DeleteCommand = OleDbDeleteCommand;
            OleDbDataAdapter.InsertCommand = OleDbInsertCommand;
            OleDbDataAdapter.SelectCommand = OleDbSelectCommand;
            OleDbDataAdapter.UpdateCommand = OleDbUpdateCommand;


OleDbConnection.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Reg"+
"istry Path=;Jet OLEDB:Database L" + 
"ocking Mode=1;Data Source=C:\\RegistrationMDB.mdb;J" + 
"et OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System datab" + 
"ase=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=S" + 
"hare Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet " + 
"OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repai" + 
"r=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";

        }

        public void SelectDB(String id) 
        { //++++++++++++++++++++++++++  SELECT +++++++++++++++++++++++++
            DBSetup();
            cmd = "Select * from Students where ID = " + iD;
            OleDbDataAdapter.SelectCommand.CommandText = cmd;
            OleDbDataAdapter.SelectCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  {
                    OleDbConnection.Open();
                    System.Data.OleDb.OleDbDataReader dr;
                    dr = OleDbDataAdapter.SelectCommand.ExecuteReader();

                    dr.Read();
                    id=iD;
                    setPassword(dr.GetValue(1)+"");
                    setEMail(dr.GetValue(2)+"");

                    setGpa(Double.Parse(dr.GetValue(3)+""));
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }

        public void InsertDB() {
        // +++++++++++++++++++++++++++  INSERT +++++++++++++++++++++++++++++++

            DBSetup();
            cmd = "INSERT into Students values(" + getID() + "," +
                             "'" + getPassword() + "'," +
                             "'" + getEMail() + "'," +
                            "'" + getGpa() +  ")";

            OleDbDataAdapter.InsertCommand.CommandText = cmd;
            OleDbDataAdapter.InsertCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.InsertCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Inserted");
                else
                    Console.WriteLine("ERROR: Inserting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                
        }
        public void updateDB() 
        {
            //++++++++++++++++++++++++++  UPDATE  +++++++++++++++++++++++++

            cmd = "Update Students set ID = '" + getID() + "'," + 
                        "Password = '" + getPassword() +    "', " +
                        "Email = '" + getEMail() + "', " +
                         "GPA = " + getGpa();

            OleDbDataAdapter.UpdateCommand.CommandText = cmd;
            OleDbDataAdapter.UpdateCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.UpdateCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Updated");
                else
                    Console.WriteLine("ERROR: Updating Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }

        public void deleteDB() 
        {
            //++++++++++++++++++++++++++  DELETE  +++++++++++++++++++++++++

            cmd = "Delete from Students where ID = " + getID();
            OleDbDataAdapter.DeleteCommand.CommandText = cmd;
            OleDbDataAdapter.DeleteCommand.Connection = OleDbConnection;
            Console.WriteLine(cmd);
            try  
            {
                OleDbConnection.Open();
                int n = OleDbDataAdapter.DeleteCommand.ExecuteNonQuery();
                if (n==1)
                    Console.WriteLine("Data Deleted");
                else
                    Console.WriteLine("ERROR: Deleting Data");
            }
            catch (Exception ex) 
            {
                Console.WriteLine(ex);
            }
            finally 
            {
                OleDbConnection.Close();
            }                    
        }

1 个答案:

答案 0 :(得分:-1)

  • 查看您的连接字符串,您似乎正在尝试使用SqlConnection对象直接打开MS Access数据库(.MDB)文件。

这引发了一些问题。

  1. 听起来您的Access数据库没有正确的连接字符串。以下是有关如何根据您使用的技术设置连接字符串的一些建议的链接:
  2. https://www.connectionstrings.com/access/

    1. 另外,如下所述,您似乎使用了错误的连接类型。有关更多信息,请参阅此相关问题:
    2. SQL connection string for microsoft access 2010 .accdb