我刚刚学习使用ADO.NET,我似乎遇到了问题.我想要做的是从表中获取数据并将其插入DataTable.Here是我的代码:
public DataTable GetCategories()
{
SqlConnection connection = null;
SqlDataReader reader = null;
DataTable categories = new DataTable();
try {
connection = new SqlConnection();
connection.ConnectionString = connectionString;
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetCategories";
reader = cmd.ExecuteReader();
categories.Columns.Add("Id", typeof(int));
categories.Columns.Add("CategoryName", typeof(int));
while (reader.Read()) {
int categoryId = (int)reader["Id"];
string categoryName = (string)reader["CategoryName"];
categories.Rows.Add(categoryId , categoryName);
}
}catch(Exception e){
DataTable error = new DataTable();
error.Columns.Add("Error");
error.Rows.Add(e.Message);
return error;
}finally{
connection.Close();
reader.Close();
}
return categories;
}
Run Code Online (Sandbox Code Playgroud)
这是我的SQL查询: …
您好我正在尝试使用ADO.NET将数据插入数据库.这是我的代码.这是我的C#代码:
public void CreateBook(FormCollection collection)
{
string name = collection["BookName"];
string author = collection["Author"];
string description = collection["Description"];
int category = int.Parse(collection["category"]);
DateTime currentDate = new DateTime();
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand("AddBook", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(createParameter(name, "@Name"));
command.Parameters.Add(createParameter(author, "@Author"));
command.Parameters.Add(createParameter(description, "@Description"));
command.Parameters.Add(createParameter(currentDate.Date, "@Date"));
command.Parameters.Add(createParameter(category, "@CategoryId"));
command.ExecuteNonQuery();
}
}
//there are 2 other overloads of this method
private SqlParameter createParameter(string parameter , string parameterName) {
SqlParameter param = new SqlParameter();
param.ParameterName = …Run Code Online (Sandbox Code Playgroud) 嗨我的任务是重构一部分代码,以便它与开放/封闭原则相对应.我设法在两种方法中应用策略模式但在一种方法中我不知道应该如何进行.
public ProductPriceCalculator(Product product)
{
this.product = product;
buyerStrategy = new BuyerStrategy();
discountStrategy = new DiscountStrategy();
}
public Price CalculatePrice()
{
price = new Price();
decimal productPrice = product.BasePrice +
(product.BasePrice * product.Addition);
decimal TVA = CalculateTVA();
price.ProductPrice = productPrice*TVA;
decimal discount = CalculateDiscount(price.ProductPrice);
price.Discount = price.ProductPrice * discount;
price.ProductPriceWithDiscount = price.ProductPrice - price.Discount;
price.TransportPrice = product.Transport.GetTransportPrice();
price.TotalPrice = price.ProductPriceWithDiscount + price.TransportPrice;
return price;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,这个方法通过使用类中的方法来初始化对象.现在这个方法没有关闭进行修改,因为如果在某些时候我必须向Price添加另一个属性,我将不得不回到这里初始化它.
我该如何正确构建此代码?
在重建项目时,我似乎遇到了一些奇怪的错误.
我有一个巫婆项目,我已经存储了所有的DomainService.如果我右键单击解决方案并选择干净的解决方案然后重建项目我得到一堆这样的错误:
错误104
命名空间"Microsoft"中不存在类型或命名空间名称"实践"(您是否缺少程序集引用?)
D:\ Projects\Backend\WebApi\DomainServices\LocalizationService.cs
我为导入此命名空间的每个文件都收到错误:
using Microsoft.Practices.ServiceLocation;
Run Code Online (Sandbox Code Playgroud)
现在我已经添加了对程序集的引用.
最奇怪的是,如果我再次重建,一切正常并且不会显示任何错误.
有没有人知道发生了什么?
嗨,我正在尝试计算表中的行数,但我alwyas回来-1.这是我的代码:
using (connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand("CountBooks", connection);
command.CommandType = CommandType.StoredProcedure;
numberOfBooks = command.ExecuteNonQuery();
}
Run Code Online (Sandbox Code Playgroud)
这是我的查询:
SELECT COUNT(*) FROM Books
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到正确的结果?
嗨我使用unity作为我的IoC框架,我有一个案例,我需要在整个应用程序中使用相同的对象实例,贝司风格创建一个单例.
假设我有这样的配置:
container.RegisterType<IValidationService, ValidationService>();
Run Code Online (Sandbox Code Playgroud)
我如何告诉团结只创建一个ValidationService实例并在我的应用程序中的任何地方使用它?
嗨我有两个字典,我需要找到一种方法将它们连接在一起.这是两种字典类型.
IDictionary<string, byte[]> dictionary1
IDictionary<IFileShareDocument, string> dictionary2
Run Code Online (Sandbox Code Playgroud)
在这两个词典中,我必须创建一个看起来像这样的第三个字典
IDictionary<IFileShareDocument, byte[]> dictionary3
Run Code Online (Sandbox Code Playgroud)
两个字典都具有完全相同的项目数,并且它们的字符串属性是链接点.
我想要的是能够写出像这样做的东西:
dictionary1.value join with dictionary2.key
where dictionary1.key == dictionary2.value
This statement should result in dictionary3.
Run Code Online (Sandbox Code Playgroud)
有什么方法可以实现这一点我似乎找不到办法做到这一点?
嗨,我正在尝试格式化我的Url,以使其看起来更加用户友好.到目前为止,我设法用" - "替换空格,但似乎有特殊的字符,如#和:显示为编码数据.这是什么我的意思是:
http://localhost:51208/Home/Details/C%23-in-Depth%2c-Second-Edition/BookId-3
Run Code Online (Sandbox Code Playgroud)
"#"符号显示为%23,","显示为%2c.我希望能够用原始符号替换此编码.
这样的方式存在吗?
嗨,我只是在学习WCF,并且正在尝试为现有的ASP.NET MVC 4应用程序创建Web服务.这是我到目前为止所做的:
我已经创建了一个WCF服务应用程序.我定义了数据契约和数据成员:
[DataContract]
public class BookData
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Author { get; set; }
[DataMember]
public string Description { get; set; }
[DataMember]
public DateTime PublicationDate { get; set; }
[DataMember]
public int CategoryId { get; set; }
[DataMember]
public decimal Price { get; set; }
[DataMember]
public string BookUrl { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我已经定义了服务合同:
[ServiceContract]
public interface IBookService
{ …Run Code Online (Sandbox Code Playgroud) 嗨,我有一个Path.Combine的问题,它没有返回corect结果.这就是我的意思.我将4个字符串组合成浴缸:
string basePath = "D:\\Programare\\Visual Studio\\TFS Workspace\\CodeArt\\Development\\CodeArt\\Backend\\WebApi\\CodeArt.WebApi\\bin\\..\\Content\\"
string userId = "4d52ec77-966a-480a-a4c3-c1ff67438fe9";
string filePath = "\\Avatar";
string storageFileName = "ffdc24a9-f553-41ce-aa33-042b07fcfdab.png";
var result = Path.Combine(basePath , userId , filePath , storageFileName);
Run Code Online (Sandbox Code Playgroud)
现在由于某种原因,我的这段代码只返回"\ Avatar\ffdc24a9-f553-41ce-aa33-042b07fcfdab.png".
我做错了什么为什么不是Path.Combine concatantes所有的字符串.