[英]Data Access Layer in c#
I want to create a data access layer in c# for first time. 我想第一次在c#中创建一个数据访问层。 So I read and used tutorials ... now I have a simple code, but this code doesn't work... 所以我阅读并使用了教程...现在我有一个简单的代码,但是此代码不起作用...
for ex: for my delete query... 例如:对于我的删除查询...
this is delete web method: 这是删除网络方法:
[WebMethod]
public void DeleteMethod(string DBName, string tableName, string attributes, string values)
{
DBName = DAL.dataAccess.DBname;
tName = DAL.dataAccess.tName;
DAL.dataAccess.Delete(attributes, values);
}
this method pass elements to dataAccess
Class and the class create query and connect to my data base. 此方法将元素传递给dataAccess
类,该类创建查询并连接到我的数据库。 the dataAccess
class code for delete query: 用于删除查询的dataAccess
类代码:
public static string DBname { get;set; }
public static string tableName { get;set; }
public static void Delete(string field, string condition)
{
connect = new SqlConnection(string.Format(_conString, DBname));
adp.SelectCommand = new SqlCommand();
adp.SelectCommand.Connection = connect;
adp.SelectCommand.CommandText = "delete from " + tName + " where " + field + " = " + condition ;
DataSet ds = new DataSet();
connect.Open();
adp.Fill(ds);
connect.Close();
}
at first I used cmd.ExecuteNonQuery();
起初我使用cmd.ExecuteNonQuery();
instead of adp.Fill(ds);
而不是adp.Fill(ds);
, but this doesn't work and get error. ,但这不起作用并出现错误。 so I changed my code to this... 所以我将代码更改为此
Now I get this error at
adp.Fill(ds);
现在,我在adp.Fill(ds);
收到此错误adp.Fill(ds);
: "Incorrect syntax near the keyword 'where'." :“关键字'where'附近的语法不正确。”
What is my code problems?? 我的代码有什么问题?
You should use an approach more like below - where you are correctly USING the connections and commands: 您应该使用类似于以下的方法-在此正确使用连接和命令:
using (var connection = new SqlConnection((string.Format(_conString, DBname))
{
using (var command = connection.CreateCommand())
{
connection.Open();
command.CommandText = "delete from " + tName + " where " + field + " = " + condition ;
command.ExecuteNonQuery();
}
}
see here for more details on why you need to use using : [ https://www.dotnetperls.com/sqlconnection] 有关为什么需要使用using的更多详细信息,请参见此处:[ https://www.dotnetperls.com/sqlconnection]
Try this code in your delete method:- 在您的删除方法中尝试以下代码:-
You can hard code the connection string instead of reading from a config file. 您可以对连接字符串进行硬编码,而不是从配置文件中读取。 Also, for me the table is Employee, for you it could be your table name. 另外,对我来说,表是Employee,对您来说,它可以是您的表名。
static void Main(string[] args)
{
tableName = "Employee";
string condition = "'User4'";
Delete("Name", condition);
}
public static void Delete(string field, string condition)
{
string _conString = ConfigurationManager.AppSettings["ConnectionString"];
using (SqlConnection connect = new SqlConnection(_conString))
{
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = new SqlCommand();
adp.SelectCommand.Connection = connect;
adp.SelectCommand.CommandText = "delete from " + tableName + " where " + field + " = " + condition;
DataSet ds = new DataSet();
connect.Open();
adp.Fill(ds);
connect.Close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.