• C#与数据库访问技术总结(七)综合示例


    综合示例

      说明:前面介绍了那么多,光说不练假把式,还是做个实例吧。

      表:首先你要准备一张表,这个自己准备吧。我们以学生表为例。

      1ExecuteScalar方法

        ExecuteScalar方法执行返回单个值的命令。例如,如果想获取Student数据库中表studentInfo的学生的总人数,则可以使用这个方法执行SQL查询:

        Select count(*) from studentInfo .

    (1) 建立Windows Application 应用程序

    (2) Form1上添加一个按钮Button控件和一个标Label签控件 

    (3) 双击按钮,自动进入代码编辑界面

      首先添加命名空间: using System.Data.SqlClient;

    (4)编写按钮的Click事件的处理事件代码:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Drawing;
    
    using System.Text;
    
    using System.Windows.Forms;
    
    using System.Data.SqlClient;
    
    namespace DataBase
    
    {
    
      public partial class Form1 : Form
    
    {
    
    public Form1()
    
    {
    
      InitializeComponent();
    
    }
    
    private void button1_Click(object sender, EventArgs e)
    
    {
    
      try
    
      {
    
        //定义命令文本
    
        string commandText = "select count(*) from studentInfo";
    
        //定义连接字符串
    
        string connString="server=(local);Initial Catalog=Student;Integrated Security=SSPI;";
    
        //string connString= "server=(local);user id=sa;Initial Catalog=Student;pwd=;";
    
        //定义Connection对象
    
        SqlConnection conn = new SqlConnection();
    
        //设置Connection对象的ConnectionString属性
    
        conn.ConnectionString = connString;
    
        //新建Command对象,此时conn对象并不需要打开连接
    
        SqlCommand cmd = new SqlCommand(commandText, conn);
    
        //打开连接
    
        conn.Open();
    
        //执行命令,返回结果
    
        string count = cmd.ExecuteScalar().ToString();
    
        //记得关闭连接
    
        conn.Close();
    
        this.label1.Text = "共有" + count + "位学生!";
    
    }
    
    catch (Exception ex)
    
    {
    
        MessageBox.Show("数据库连接失败" + ex.Message);
    
    }
    
        }
    
      }
    
    }

      执行结果界面如图:

       

    分析代码:

    1是引入命名空间:System.Data.SqlClient,表示将使用SQL Server.NET 数据提供程序: using System.Data.SqlClient;

    2是 按钮button1_Click单击事件中首先新建立了连接并设置了其连接字符串属性:

    string connString="server=(local);Initial Catalog=Student;Integrated Security=SSPI;";

    //string connString= "server=(local);user id=sa;Initial Catalog=Student;pwd=;";

    //定义Connection对象

    SqlConnection conn = new SqlConnection();

    //设置Connection对象的ConnectionString属性

    conn.ConnectionString = connString;

    第三步,新建Command 对象,并将命名文本和连接对象传递给其构造函数:

    SqlCommand cmd = new SqlCommand(commandText, conn);

    其中,commandText为最初定义的命名文本:

    string commandText = "select count(*) from studentInfo";

    此时conn对象没有打开,因为这不是必须的。

    第四步 现在需要执行操作了,所以首先要打开连接,然后执行操作:

    conn.Open();

    string count = cmd.ExecuteScalar().ToString();

    由于ExecuteScalar()方法返回类型为object,因此使用了ToString()方法将其转换为string以后赋值给count

    注意:一般使用ExecuteScalar()方法时都必须用到类型转换。

    第五步数据库访问完毕以后应该立即关闭连接,这是一个好习惯:

    corm.Close();

    第六步最后将读取的信息通过label1显示出来:

    this.label1.Text="共有"+count+"位学生!";

    上面的代码中并没有指定Command对象的CommandType属性,这时CommandType的值将为默认的Text,当然也可以通过如下代码显示指定其类型,但这不是必须的。

    cmd.CommandType=CommandType.Text;

  • 相关阅读:
    myeclipse 自定义视图Customize Perspective 没有反应
    myEclipse工具栏设置新建类、包、接口图标
    eclipse手动添加SVN插件
    ui 库vue app
    sublime text3安装emmet插件
    Sublime Text 3最受欢迎的配色主题theme-超炫
    516D Drazil and Morning Exercise
    506C Mr. Kitayuta vs. Bamboos
    555E Case of Computer Network
    576D Flights for Regular Customers
  • 原文地址:https://www.cnblogs.com/zi-xing/p/4033359.html
Copyright © 2020-2023  润新知