小编cat*_*ple的帖子

将对象列表插入SQL Server表

我想在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)

c# sql sql-server

8
推荐指数
2
解决办法
2万
查看次数

使用LINQ代替两个for循环

我想将这两个"丑陋" 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)

c# linq

4
推荐指数
1
解决办法
631
查看次数

AngularJS将值从指令链接函数传递到控制器

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)

javascript angularjs

4
推荐指数
1
解决办法
3303
查看次数

这行打字稿是什么意思?

我正在从头学习一些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)

javascript frontend typescript

3
推荐指数
1
解决办法
602
查看次数

如何在其他JS文件/模块中调用/使用此模块

我最近阅读了一些JS模块设计模式.我遇到了这个小代码片段,如下所示.

(function(window) {
    var Module = {
        data: "I'm happy now!"
    };

    window.Module = Module;
})(window);
Run Code Online (Sandbox Code Playgroud)

我还不太了解这段代码,我的问题是:

  • 如何在这个特定的JS文件之外使用/调用此模块?我需要为这个模块分配一个变量吗?例如var module1 =(...)(...);
  • 谁能解释一下这里的窗口参数代表什么?
  • 在同一个文件中有两个/三个这样的模块是一个好习惯吗?

javascript design-patterns function

2
推荐指数
1
解决办法
3726
查看次数

从Javascript中的两个嵌套数组中获取一个对象

我想从两个数组中获得一个对象,我是按照以下方式完成的.

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 frontend data-structures

0
推荐指数
1
解决办法
39
查看次数