zoukankan      html  css  js  c++  java
  • 在VS2019里面 C# 和SqlSerer的勾兑

    1)WIN10  64 专业版系统,Sqlserver2008 ,全部安装好

    2)通过SQL Server Management Studio  新建一个数据库 ,然后在库里面新建一个表

    3)在VS2019里面,添加Sql Server

     4)打开属性

    就可以找到链接字符串

     以上完成准备工作,下面就可以进入代码环节

    (Data Source=DESKTOP-1GHN2NO;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False)(链接字符,各有区别,不必在意)

    https://www.cnblogs.com/vaevvaev/p/6898891.html

    5)在VS2019 里面新建C#的窗口程序

    命名空间如下:

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

    缺啥 补啥

    6)SqlConnection myconnection;//定义一个数据库连接对象

    7)打开数据, 就是一个按钮的点击事件响应函数

    private void button1_Click(object sender, EventArgs e)
    {
    try

    {

    myconnection = new SqlConnection("Data Source=DESKTOP-1GHN2NO;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite");

    myconnection.Open(); //打开数据库

    label1.Text = "数据库连接成功!";

    }

    catch (Exception ee)

    {

    MessageBox.Show("数据库连接失败!" + ee.ToString());

    }
    }

    8)插入数据

    private void button_insert_Click(object sender, EventArgs e)
    {
    try
    {
    //String Gsz_Time = DateTime.Now.Hour.ToString()+'_'+ DateTime.Now.Minute.ToString()+ '_'+ DateTime.Now.Second.ToString();

    string GSZ_ID = "7";
    string GSZ_Time = DateTime.Now.Hour.ToString() + '_' + DateTime.Now.Minute.ToString() + '_' + DateTime.Now.Second.ToString();
    string GSZ_ACC = "7";
    string insertCommand = "insert into Table_Gsz (ID,Time,Data_ACC) values('"+ GSZ_ID + "','"+GSZ_Time+"','"+GSZ_ACC+"')" ;
    SqlCommand command = new SqlCommand(insertCommand, myconnection);
    command.ExecuteNonQuery();

    }
    catch (Exception ee)
    {
    MessageBox.Show("插入数据库!" + ee.ToString());

    }

    }

    时间相关:https://www.cnblogs.com/huanglong/archive/2012/03/26/2417487.html

    9)查询

    private void button3_Click(object sender, EventArgs e)
    {
    DataSet ds = new DataSet();
    try
    {
    string sql = "select * from Table_Gsz";
    SqlDataAdapter adp = new SqlDataAdapter(sql, myconnection);
    adp.Fill(ds);
    }
    catch (Exception ee)
    {
    MessageBox.Show("查询数据库!" + ee.ToString());
    }

     10)关闭

    private void button2_Click(object sender, EventArgs e)
    {
    try
    {
    myconnection.Close();
    myconnection.Dispose();
    }
    catch (Exception ee)
    {
    MessageBox.Show("关闭数据库连接!" + ee.ToString());
    }


    }

  • 相关阅读:
    JS——变量和函数的预解析、匿名函数、函数传参、return
    JS——函数
    JS——操作元素属性
    JS——变量声明、变量类型、命名规范
    JS——三种嵌入页面的方式
    CSS——弹性盒模型
    CSS——相对定位、绝对定位、固定定位
    CSS3——PC以及移动端页面适配方法(响应布局)
    [20180528]校内模拟赛
    Leetcode Majority Element系列 摩尔投票法
  • 原文地址:https://www.cnblogs.com/wenluderen/p/14887942.html
Copyright © 2011-2022 走看看