zoukankan      html  css  js  c++  java
  • 在C#下使用sql语句(查询,插入,更新,删除……)

    需要说明的是,我使用的是sql server 2000为服务器。

    按照以下的几步,就可以很顺利的连接到服务器,执行基本的sql操作了。

    第一步 连接服务器
    SqlConnection thisConnection = new SqlConnection(@"Server = (local); Integrated Security = True;" + "Database = hospital");
    thisConnection.Open();

    第二步 新建命令

    SqlCommand thiscommand = thisConnection.CreateCommand();

    第三步 给问题文本赋值

    thiscommand.CommandText = "insert into Users(Telephone) values('02787546321')"

    这里的字符串就是需要执行的sql命令

    第四步 执行命令

    分为三种命令,相应调用不同的方法:

    1 不需要查询的(插入,更新,删除)

    thiscommand.ExecuteNonQuery();

    该函数会返回收到影响的总行数。

    2 只需要查询一个值的

    thiscommand.ExecuteScalar();

    该函数会返回使用的sql语言查询的结果

    3 需要同时查询得到多个值的

    SqlDataReader QuesReader = thiscommand.ExecuteReader();  //新建一个SqlDataReader 
    QuesReader.Read();        //读取一行数据到Reader中
    thisQues[0] = (string)QuesReader["Text"];  //将Reader中的数据读取走
    QuesReader.Close();   //关闭Reader

    第五步 关闭连接

    thisConnection.Close();

  • 相关阅读:
    Python 内置函数 —— format
    命名集 —— 名字结构
    命名集 —— 名字结构
    存储与主板的外设接口
    存储与主板的外设接口
    验证码的认识
    验证码的认识
    windows 路径
    windows 路径
    极限的求法
  • 原文地址:https://www.cnblogs.com/csonezp/p/2835472.html
Copyright © 2011-2022 走看看