使用LINQ添加数据 [英] Adding Data using LINQ

查看:116
本文介绍了使用LINQ添加数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用LINQ和Colections向我的SQL Server数据库添加数据.

在前端,我使用的是列表框控件,它在其中显示添加的数据,但在后端,数据库未更新.

下面是源代码,没有错误或异常提示.

Hello All,

I am trying to add data to my SQL Server database using LINQ and Colections.

In the front end, I am using a List Box Control, in which it shows the data as added, but in the backend, the database is not updated.

Below is the source code, There is no error or exception prompted.

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<test1> tests = GetNames();
        var q = from c in tests
                
                select c;
        foreach (var i in q)
        {
            ListBox1.Items.Add(i.FirstName + i.LastName);
        }

        
        

    }

     List<test1> GetNames()
    {
        List<test1> t = new List<test1>();
        t.Add(new test1("Albert", "Einstein"));
        return t;
     
    }
}

</test1></test1></test1></test1>



上面的源代码用于Default.aspx.cs文件.

我还有另一个* .cs文件.下面是代码.



The above source code was for the Default.aspx.cs file.

I have another *.cs file. Below is the Code.

using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

public class test1
{
    private string first_name;
    public string FirstName
    {
        get { return first_name; }
        set { first_name = value; }
    }
    private string last_name;
    public string LastName
    {
        get { return last_name; }
        set { last_name = value; }
    }

	public test1(string FirstName,string LastName)
	{
        first_name = FirstName;
        last_name = LastName;
    
	}
}




数据库中的名字和姓氏中只有2个字段.

请协助
非常感谢和问候




There are only 2 fields in the database First Name and Last Name.

Kindly Assist
Many Thanks and Regards

推荐答案

您好,

您发布的内容似乎没有任何代码可以实际写入数据库.您不是在使用LINQ to SQL,而是在使用LINQ to Objects.

可能我建议阅读 [
Hi there,

It looks as though there is no code in what you''ve posted to actually write to the database. You''re not using LINQ to SQL, you''re using LINQ to objects.

Might I suggest a read[^]?

It will be worth going through start-to-finish to get an understanding about what is required to work with LINQ as it relates to creating data in the database.

The key thing will be that with the help of a DBML file, you''ll not need to create classes from scratch (there will be partial classes created for you) and updating the database - including writing new data to it - is as simple as calling a method named SubmitChanges() on your DataContext.

Cheers.


string connectionString = ConfigurationManager.ConnectionStrings["Data Source=M-EDE94949520E4''''SQLEXPRESS;Initial Catalog=test;Integrated Security=True"].ToString();



这根本不可能是您的web.config中的有效条目.您需要找到连接字符串的名称,并替换[]之间的文本以设置正确的密钥.

干杯.



This is not likely at all a valid entry in your web.config. You need to find the name of your connection string and replace the text between the [] to set the correct key.

Cheers.


这篇关于使用LINQ添加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆