关于CRUD和查看数据的问题是否有更简单的方法? [英] Question about CRUD and viewing data and is there and easier way?
问题描述
您好我有关于创建插入查询的问题。有没有办法可以用int和datetime做Convert.DBNull?另外我想知道我应该用C#WPF做CRUD的最有效方法。我目前正在使用listview从我的数据库中查看我的数据我使用SqlCommand和SqlDataReader将我的数据放入列表< myobject>我使用foreach将列表添加到列表视图中。我想知道我是否应该使用Datatable而不是DataGrid?
我想知道我是否可以使用像这样的Create方法?
Hello I have a question about creating an insert query. Is there a way I can do Convert.DBNull with an int and a datetime? Also I would like to know the most efficient way I should be doing CRUD with C# WPF. I am currently using a listview to view my data from my DB I use a SqlCommand and a SqlDataReader to put my data into a list<myobject> and I use a foreach to add the list to the listview. I am wondering if I should be using a Datatable instead and possibly a DataGrid?
I am wondering if I could use a Create method like this?
public static void CreateProduct(Product product)
{
using (SqlConnection connection = ConnectionString.GetSqlConnection())
{
connection.Open();
string sqlCreate = "INSERT INTO Products (ProductName, ProductPrice, ProductQuantity, " +
"ProductTax, ProductTotal, ProductListedDate, ProductPurchasedDate, UserID, ReceiptID " +
"VALUES(@pn, @pp, @pq, @pta, @pto, @pld, @ppd, @uid, @rid)";
using (SqlCommand cmdCreate = new SqlCommand(sqlCreate, connection))
{
cmdCreate.Parameters.AddWithValue("@pn", product.ProductName);
cmdCreate.Parameters.AddWithValue("@pp", product.ProductPrice);
cmdCreate.Parameters.AddWithValue("@pq", product.ProductQuantity);
cmdCreate.Parameters.AddWithValue("@pta", product.ProductTax);
cmdCreate.Parameters.AddWithValue("@pto", product.ProductTotal);
cmdCreate.Parameters.AddWithValue("@pld", product.ProductListedDate);
cmdCreate.Parameters.AddWithValue("@ppd", product.ProductPurchasedDate);
cmdCreate.Parameters.AddWithValue("@uid", product.UserID); // ?? Convert.DBNull);
cmdCreate.Parameters.AddWithValue("@rid", product.ReceiptID);
cmdCreate.ExecuteNonQuery();
}
}
}
//But this is the way I have my current create but I was wondering if I didn’t need @@IDENTITY
public static int CreateNewUser(User newUser)
{
string SQLinsertQuery = "INSERT INTO Users (Username, Password, IsAdmin, UserCreatedDate) " +
"VALUES(@username, @password, @isadmin, @usercreateddate)";
//SQL command
SqlCommand cmdCreate = new SqlCommand(SQLinsertQuery, connection);
cmdCreate.Parameters.AddWithValue("@username", newUser.Username);
cmdCreate.Parameters.AddWithValue("@password", newUser.Password);
cmdCreate.Parameters.AddWithValue("@isadmin", newUser.IsAdmin);
cmdCreate.Parameters.AddWithValue("@usercreateddate", newUser.UserCreatedDate);
try
{
connection.Open();
cmdCreate.ExecuteNonQuery();
string SQLselect = "SELECT @@IDENTITY FROM Users";
SqlCommand cmdSelect = new SqlCommand(SQLselect, connection);
int userId = Convert.ToInt32(cmdSelect.ExecuteScalar());
return userId;
}
catch (SqlException sql)
{
sql.Message.ToString();
throw sql;
}
catch (Exception ex)
{
ex.Message.ToString();
return -1;
}
finally { connection.Close(); }
}
我尝试过:
使用listview和其他创建方法
What I have tried:
using listview and other create method
推荐答案
是;开始使用ORM。我使用实体框架。
Yes; start using an ORM. I use Entity Framework.
这篇关于关于CRUD和查看数据的问题是否有更简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!