我想在sql server表中插入一个对象列表.但是,目前,每次插入记录行时都必须打开和关闭sql连接.
我只是想知道是否有一种方法可以一次插入记录列表中的所有对象?这是代码片段.
public void InsertDataToDb()
{
string connectionString = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
var records = GetRecords();
using (SqlConnection conn = new SqlConnection(connectionString))
{
SqlCommand cmd =
new SqlCommand(
"INSERT INTO TableName (param1, param2, param3) VALUES (@param1, @param2, @param3)");
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
foreach (var item in records)
{
cmd.Parameters.AddWithValue("@param1", item.param1);
cmd.Parameters.AddWithValue("@param2", item.param2);
cmd.Parameters.AddWithValue("@param3", item.param3);
conn.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我想将这两个"丑陋" for循环转换为LINQ表达式.谁能帮我吗?我对LINQ很新.提前致谢!
foreach (Edge edge in distinctEdge)
{
var c = 0;
foreach(Edge e in EdgeList)
{
if(e.target == edge.target && e.source == edge.source)
{
c++;
}
}
edge.value = c;
}
Run Code Online (Sandbox Code Playgroud) Angular的新手。非常简单的问题。我有以下代码。
我只想显示下面的文件数。我将fileCount变量绑定到作用域,但是它不起作用。
var app = angular.module('fileUploader', []);
app.controller('upload', function($scope){
$scope.fileCount = 0;
})
.directive("customDirective", function(){
return{
link: function(scope, el, attrs){
el.bind("change", function(event){
console.log(event.target.files.length);
scope.fileCount = event.target.files.length;
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>
<body>
<div ng-app="fileUploader" ng-controller="upload">
<input custom-Directive type="file"/>
<p>The file count is: {{fileCount}}</p>
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
我正在从头学习一些Typescript.这是他们官方网站上的一些代码.我对下面的一行感到困惑.这个声明在这里意味着什么?等号后的{}表示返回类型无效?
var validators: { [s: string]: Validation.StringValidator; } = {};
Run Code Online (Sandbox Code Playgroud)
////////////////////////////////////////////////// /////////////////////////////////////////
module Validation {
export interface StringValidator {
isAcceptable(s: string): boolean;
}
var lettersRegexp = /^[A-Za-z]+$/;
var numberRegexp = /^[0-9]+$/;
export class LettersOnlyValidator implements StringValidator {
isAcceptable(s: string) {
return lettersRegexp.test(s);
}
}
export class ZipCodeValidator implements StringValidator {
isAcceptable(s: string) {
return s.length === 5 && numberRegexp.test(s);
}
}
}
// Some samples to try
var strings = ['Hello', '98052', '101'];
// Validators to use
var validators: { …
Run Code Online (Sandbox Code Playgroud) 我最近阅读了一些JS模块设计模式.我遇到了这个小代码片段,如下所示.
(function(window) {
var Module = {
data: "I'm happy now!"
};
window.Module = Module;
})(window);
Run Code Online (Sandbox Code Playgroud)
我还不太了解这段代码,我的问题是:
我想从两个数组中获得一个对象,我是按照以下方式完成的.
for (var j = 0; j < rawDataRows.length; j++) {
for (var i = 0; i < categories.length; i++) {
var category = categories[i];
var rowValue = rawDataRows[j];
// here I do got the right value for category
console.log(category);
console.log(rowValue);
// but the following line doesn't interpret category as a variable
formattedDataRows.push({category: rowValue});
}
}
Run Code Online (Sandbox Code Playgroud)
我假设我可以得到类似的东西:
[{"category1": "value1"},{"category2": "value2"}, {"category3": "value3"}]
Run Code Online (Sandbox Code Playgroud)
然而,事实证明我得到了:
[{"category": "value1"}, {"category": "value2"}, {"category": "value3"}]
Run Code Online (Sandbox Code Playgroud)
谁能指出我错在哪里?此外,如果您有更好的方法来实现目标,请发表评论.Javascript只有jQuery或其他框架.谢谢!
javascript ×4
c# ×2
frontend ×2
angularjs ×1
function ×1
linq ×1
sql ×1
sql-server ×1
typescript ×1