典型代码-linq 基本的增加记录操作

来源:互联网 发布:html可以连接数据库吗 编辑:程序博客网 时间:2024/06/16 08:59

添加项目

添加窗体

窗体添加dataGridView1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataClasses1DataContext da = new DataClasses1DataContext();


            t001 cus = new t001();
            cus.bukrs = "8002";
            cus.text = "文华传媒有限公司";
            cus.addr = "河西路大浦街33号五里";
            //这两句话有点像以前学过的事务,只有在添加完成之后才会写入数据库
            da.t001.InsertOnSubmit(cus);
            da.SubmitChanges();

            var query = from s in da.t001
                        where s.bukrs != "2222"
                        select s;
            dataGridView1.DataSource = query;

        }
    }
}


app.config的代码

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WindowsFormsApplication1.Properties.Settings.sapConnectionString"
           
          
            
            
            
              connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\DataBase\sap.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
          
             providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

再建立一个空网站

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data.SqlClient;

using System.Data;

 

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        string str = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\DataBase\\sap.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            SqlConnection myConnection = new SqlConnection(str);

            myConnection.Open();

            SqlCommand myCommand = new SqlCommand("select * from t001", myConnection);

            SqlDataAdapter Adapter = new SqlDataAdapter();

            Adapter.SelectCommand = myCommand;

            DataSet myDs = new DataSet();

            Adapter.Fill(myDs);

            Response.Write("<h3>用使用DataSet显示CustomerInfo数据表内容</h3>");

            Response.Write("<table border=1 cellspacing=0 cellpadding=2>");

            DataTable myTable = myDs.Tables[0];

            Response.Write("<tr bgcolor=yellow>");

            foreach (DataColumn myColumn in myTable.Columns)

            {

                Response.Write("<td>" + myColumn.ColumnName + "</td>");

            }

            Response.Write("</tr>");

            foreach (DataRow myRow in myTable.Rows)

            {

                Response.Write("<tr>");

                foreach (DataColumn myColumn in myTable.Columns)

                {

                    Response.Write("<td>" + myRow[myColumn] + "</td>");

                }

                Response.Write("</tr>");

            }

            Response.Write("</table>");

            myConnection.Close();

 

 

    }
}

 

0 0