У меня есть файл Connection.cs в папке App_Code Microsoft Visual Web Developer 2010, но он предупреждает меня об ошибке:
Не удалось найти тип или имя пространства имен 'Connection' (вам не хватает директивы using или ссылки на сборку?)
(«Соединение» - это имя класса в файле Connection.cs)
Я не очень разбираюсь в ссылках на файлы и не понимаю описания справки по ошибке в MSDN. Как я могу это исправить?
Изменить:
Файл Connection.cs:
public class Connection
{
public void Insert(OleDbConnection cnctn, string tableName, string[] inputs, char[] types)
{
// new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + this.dbPath);
string sql = "INSERT INTO " + tableName + " VALUES (";
for (int i = 0; i < inputs.Length - 1; i++)
sql += "@p" + i + ",";
sql += "@p" + (inputs.Length - 1) + ")";
cnctn.Open();
OleDbCommand cmnd = new OleDbCommand(sql, cnctn);
for (int i = 0; i < inputs.Length; i++)
{
if (types[i] == 'c')
cmnd.Parameters.Add("@p" + i, OleDbType.VarChar);
else if (types[i] == 'n')
cmnd.Parameters.Add("@p" + i, OleDbType.VarNumeric);
cmnd.Parameters["@p" + i].Value = inputs[i];
}
cmnd.ExecuteNonQuery();
}
public bool DoesExists(OleDbConnection cnctn, string tableName, string condition)
{
// new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + this.dbPath);
cnctn.Open();
OleDbCommand cmnd = new OleDbCommand("SELECT * FROM " + tableName + " WHERE " + condition, cnctn);
bool temp = cmnd.ExecuteReader().Read();
cnctn.Close();
return temp;
}
public DataSet SetDataSet(OleDbConnection cnctn, string tableName, string sql)
{
cnctn.Open();
DataSet ds = new DataSet();
OleDbCommand cmnd = new OleDbCommand(sql, cnctn);
OleDbDataAdapter da = new OleDbDataAdapter(cmnd);
da.Fill(ds, tableName);
cnctn.Close();
return ds;
}
public void Execute(OleDbConnection cnctn, string sql)
{
cnctn.Open();
OleDbCommand cmnd = new OleDbCommand(sql, cnctn);
cmnd.ExecuteNonQuery();
cnctn.Close();
}
}
Один из файлов, использующих этот класс:
public partial class register : System.Web.UI.Page
{
private string[] errors = new string[5];
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Form["submit"] != null)
{
string[] inputs = {
Request.Form["userName"],
Request.Form["names"],
Request.Form["password"],
Request.Form["email"],
Request.Form["birthDate"]
};
string[] regexString = {
@"[^&<>\n]+( [^&<>\n]+)*",
@"['\-A-z]{2,}( ['\-A-z]{2,})+",
".{5,}",
@"\w+@\w+(\.\w+)+",
@"(\d{4}-\d\d-\d\d)?"
};
char[] types = { 'u', 'i', 'i', 'i', 'd' };
bool flag = true;
OleDbConnection cnct = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + Server.MapPath("App_Data/WMUdb.accdb"));
for (int i = 0; i < inputs.Length; i++)
{
if (!(new Validation(inputs[i], regexString[i], types[i], cnct, "members").isValid()))//this is another class frome the App_Code, with the same problem.
{
//error alert
flag = false;
}
}
if (flag)
{
string[] values = new string[6];
for (int i = 0; i < inputs.Length - 1; i++)
values[i] = inputs[i];
values[5] = "1";
Connection cnctn = new Connection();
cnctn.Insert(cnct, "members", values, new char[6] { 'c', 'c', 'c', 'c', 'c', 'n' });
Session["userName"] = inputs[0];
Session["adminLevel"] = 1;
Response.Redirect("homepage.aspx");
}
}
}
}
2-е изменение:
Я должен упомянуть, что файл App_Code не был создан автоматически при создании проекта, а был добавлен вручную - я использовал «Добавить элемент ...», чтобы создать папку с этим именем, добавил к нему «Connection.cs» и все .