![](/img/trans.png)
[英]Type or namespace name could not be found (missing using directive or assembly reference?)
[英]The type or namespace name 'dbConnection' could not be found (are you missing a using directive or an assembly reference?)
我是C#和Visual Studio的新手。 我正在嘗試編寫一個小型應用程序,可以在其中插入/更新/刪除和選擇MySQL數據庫中的記錄。
我確實有使用PHP和其他語言進行編程的經驗,但這是我第一次使用Windows應用程序。
我寫了一個類來處理與MySQL的打開/關閉連接並處理查詢。
類名是dbConnetion
,它位於Clases\\dbConnetion.cs
在要使用該類的表單上,我放在頂部
using dbConnetion;
但是由於某種原因,我在嘗試添加課程時一直遇到錯誤
Error 1 The type or namespace name 'dbConnection' could not be found (are you missing a using directive or an assembly reference?) C:\Users\User\C# Projects\POS\POS\newDepartment.cs
這是我遇到的截圖
這是文件的屏幕截圖
這是我的班級代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace POS
{
public class dbConnetion
{
//private OdbcConnection conn;
private readonly string mServer;
private readonly string mDatabase;
private readonly string mUid;
private readonly string mPassword;
private readonly string mPort;
private readonly string conn_string;
public dbConnetion()
{
mServer = "localhost";
mDatabase = "pos";
mUid = "root";
mPassword = "";
mPort = "3306";
conn_string = String.Format("server={0};user={1};database={2};port={3}password={4};", mServer, mUid, mDatabase, mPort, mPassword);
}
//Start connection to database
private bool startConnection(MySqlConnection mConnection)
{
try
{
mConnection.Open();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
return false;
}
}
//Close connection
private bool closeConnection(MySqlConnection mConnection)
{
try
{
mConnection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public MySqlDataReader getDataSet(string query)
{
MySqlConnection conn = new MySqlConnection(conn_string);
if (startConnection(conn) == true)
{
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader dataset = cmd.ExecuteReader();
closeConnection(conn);
}
return dataset;
}
public void processQuery(string strSQL, List<MySqlParameter> pars)
{
MySqlConnection conn = new MySqlConnection(conn_string);
if (startConnection(conn) == true)
{
MySqlCommand cmd = new MySqlCommand(strSQL, conn);
foreach (MySqlParameter param in pars)
{
cmd.Parameters.Add(param);
}
cmd.ExecuteNonQuery();
closeConnection(conn);
}
}
}
}
我該如何解決這個問題? 以及如何正確包含該類?
這里有多個問題:
using
指令的一類 。 您無法做到這一點(無論如何,在C#6之前)-您要么為類指定別名(在此處不需要),要么只是指定名稱空間以導入該名稱空間中的所有類型。 (以便您可以通過它們的簡單名稱來引用它們。) dbConnetion
,但是您已經using dbConnection;
POS
名稱空間中,但是您嘗試使用它而不指定名稱空間 從根本上說,你並不需要using
如你想在同一個命名空間中使用它的代碼指令它在所有 。 但是,它確實需要在一個項目中-與您嘗試使用的項目相同,或者是引用的項目。
您還應該閱讀.NET命名約定 -目前您的名字都沒有跟隨它們。
同樣,保持連接任意長的時間也是一個壞主意-您應該打開,使用它,關閉它。 使用using
語句可確保在結束時將其關閉,即使拋出異常也是如此。
您的班級名稱和您在using語句中使用的班級名稱不兼容
dbConnetion
dbConnection
另外,如果您在同一個命名空間中, 則無需 使用
您可以using
命名空間而不是類。 如果使用方的類不在同一個名稱空間中,則可以交換:
using dbConnection;
對於
using POS;
但是,在這種情況下,只需完全刪除該行,然后將類文件移動到項目中即可,而不是僅將其放在解決方案的文件夾中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.