adonet基础示例分享(adonet连接数据库)

时间:2024-01-15 23:15:44 

adonet基础示例分享


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;

namespace ADONET基础 {
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ////using() 自动关闭数据库,回收资源。
            ////SqlConnection为建立和数据库连接的对象。
            //using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=myself;User Id=sa;Password=123;"))
            //{
            //    conn.Open();//打开连接
            //    //通过连接,创建一个向数据库发命令的对象SqlCommand
            //    using (SqlCommand cmd = conn.CreateCommand())//释放资源。
            //    {
            //        //CommandText为要执行的SQL的语句
            //        cmd.CommandText = "Insert into student(学号,姓名) values(110,'张五')";
            //        //ExecuteNonQuery一般用来执行Update Delete Insert 语句。
            //        cmd.ExecuteNonQuery();//执行上面的SQL语句。
            //    }
            //}

            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "select count(*) from student where 入学成绩<570";
                    //cmd.CommandText = "select count(*) from student where 入学成绩<570";
                    ////ExecuteScalar一般用来执行有且只有一行一列返回值的SQL语句。
                    //int i = (int)cmd.ExecuteScalar();
                    //MessageBox.Show(i+"人成绩小于570分");
                    cmd.CommandText = "Insert into T_Student(Name,Age) output inserted.Id values('张顾',18);";
                    long i = (long)cmd.ExecuteScalar();
                    MessageBox.Show("Id为"+i);
                }
            }
            MessageBox.Show("执行完成");
        }
    }
}
标签:adonet,数据库
0
投稿

猜你喜欢

  • python实现excel转置问题详解

    2023-06-27 23:27:27
  • IE6,7下实现white-space:pre-wrap;

    2009-12-31 18:30:00
  • 深入浅析Vue中mixin和extend的区别和使用场景

    2024-05-29 22:42:43
  • 如何用Python将图片转为字符画

    2022-06-15 09:26:06
  • Python学习笔记_数据排序方法

    2022-03-07 01:28:33
  • Python将8位的图片转为24位的图片实现方法

    2021-07-31 12:11:42
  • 揭秘SQL Server 2014有哪些新特性(1)-内存数据库

    2024-01-26 05:42:47
  • 301转向和网址规范化

    2007-09-26 14:00:00
  • Django使用HTTP协议向服务器传参方式小结

    2023-03-08 11:31:20
  • python如何利用cv2.rectangle()绘制矩形框

    2021-03-14 10:12:51
  • Facebook:产品设计评价体系解密

    2011-05-24 17:13:00
  • Python 多线程Threading初学教程

    2022-12-17 18:53:43
  • python绘制封闭多边形教程

    2023-08-12 04:55:00
  • ASP中将Excel数据导入到Access

    2009-02-02 09:15:00
  • 举例讲解Linux系统下Python调用系统Shell的方法

    2023-08-25 00:04:46
  • 使用ODBC接口访问MySQL

    2009-02-13 13:59:00
  • 服务端XMLHTTP(ServerXMLHTTP in ASP)基本应用(下)

    2008-11-11 12:59:00
  • php中防止SQL注入的最佳解决方法

    2023-08-19 21:52:20
  • python爬取m3u8连接的视频

    2023-06-18 13:40:21
  • MySQL中的长事务示例详解

    2024-01-28 11:16:51
  • asp之家 网络编程 m.aspxhome.com