分享

在C#下使用sql语句(查询,插入,更新,删除……)

 牛人的尾巴 2015-11-02

需要说明的是,我使用的是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();

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多