C#操作各种执行sql的方法含存储过程操作
2010-04-14 15:08
513 查看
using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Xml; using System.Data; namespace MyDbTest { class Program { static void Main(string[] args) { SqlConnection thisConnection = new SqlConnection( @"Data Source=localhost;Initial Catalog=CSGL;Persist Security Info=True;User ID=test;Password=test"); thisConnection.Open(); SqlCommand myCommand = new SqlCommand("P_Test", thisConnection); myCommand.CommandType = CommandType.StoredProcedure; //添加输入查询参数、赋予值 myCommand.Parameters.Add("@id", SqlDbType.Int); myCommand.Parameters["@id"].Value = "120"; //添加输出参数 myCommand.Parameters.Add("@Rowcount", SqlDbType.Int); myCommand.Parameters["@Rowcount"].Direction = ParameterDirection.Output; myCommand.ExecuteNonQuery(); //得到存储过程输出参数 Console.WriteLine(" 存储过程的参数"+ myCommand.Parameters["@Rowcount"].Value.ToString()); thisConnection.Close(); Console.ReadLine(); //SqlCommand thisCommand = thisConnection.CreateCommand(); //thisCommand.CommandText = "select count(*) from stu"; ////ExecuteScalar:执行只返回一个值的SQL命令。 //object countResult = thisCommand.ExecuteScalar(); //Console.WriteLine("Count of Customers={0}", countResult); //thisConnection.Close(); //Console.ReadLine(); //SqlCommand thisCommand = thisConnection.CreateCommand(); //thisCommand.CommandText = "update stu set snm='haha' where id=120"; ////Inset,Update,Delelte的数据修改操作也不返回任何数据, ////我们对这些命令感兴趣的是修改操作影响的行数,可以用ExecuteNonQuery()方法 //int rowsAffected = thisCommand.ExecuteNonQuery(); //Console.WriteLine("Rows Updated={0}", rowsAffected); //thisConnection.Close(); //Console.ReadLine(); //SqlCommand thisCommand = thisConnection.CreateCommand(); //thisCommand.CommandText = "select * from stu FOR XML AUTO,root('RPG')"; ////Inset,Update,Delelte的数据修改操作也不返回任何数据, ////我们对这些命令感兴趣的是修改操作影响的行数,可以用ExecuteNonQuery()方法 //XmlReader xml = thisCommand.ExecuteXmlReader(); //XmlDocument xmltext = new XmlDocument(); //xmltext.Load(xml); //while (xml.Read()) //{ // Console.Write("Element: " + xml.Name); // if (xml.HasAttributes) // { // for (int i = 0; i < xml.AttributeCount; i++) // { // xml.MoveToAttribute(i); // Console.Write(" {0}: {1}", xml.Name, xml.Value); // } // xml.MoveToElement(); // } //} //Console.WriteLine(xmltext.InnerXml); //thisConnection.Close(); //Console.ReadLine(); /*** //// 存储过程------------------------------ SqlCommand thisCommand = thisConnection.CreateCommand(); //命令类型为存储过程 thisCommand.CommandType = CommandType.StoredProcedure; //存储过程名称 thisCommand.CommandText = "Ten Most Expensive Products"; //执行存储过程 SqlDataReader thisReader = thisCommand.ExecuteReader(); //显示结果 while (thisReader.Read()) { Console.WriteLine("/t{0}/t{1}", thisReader["TenMostExpensiveProducts"], thisReader["UnitPrice"]); } thisReader.Close(); thisConnection.Close(); Console.ReadLine(); * ***/ //SqlCommand thisCommand = thisConnection.CreateCommand(); ////命令类型为存储过程 //thisCommand.CommandType = CommandType.StoredProcedure; ////存储过程名称 //thisCommand.CommandText = "GetAll"; ////执行存储过程 //SqlDataReader thisReader = thisCommand.ExecuteReader(); ////显示结果 //while (thisReader.Read()) //{ // Console.WriteLine("/t{0}/t{1}", thisReader["TenMostExpensiveProducts"], thisReader["UnitPrice"]); //} //thisReader.Close(); //thisConnection.Close(); //Console.ReadLine(); } } }
相关文章推荐
- C#执行存储过程并将结果填充到GridView的方法
- c#执行插入sql 时,报错:异常信息:超时时间已到。在操作完成之前超时时间已过或服务器未响应
- 定时执行某操作的存储过程(sql作业)(1)
- C#中的类SqlCommand对象使用方法ExecuteNonQuery()调用SQLServer存储过程时,存储过程执行成功,数据发生改变,但是返回-1
- C#中foreach出现“集合已修改 可能无法执行枚举操作”的解决方法
- C#动态操作SQL的方法!
- C#(.net)中的一次连接数据库执行多条sql语句(两种方法)
- c#实现用SQL池,多线程定时批量执行SQL语句的方法
- Microsoft SQL Server 2000在复制的时候包含两个过程,攻击者可以操作输入提供恶意参数给存储过程,可导致执行任意命令。
- shell脚本操作mysql数据库-e参数执行各种sql
- MySQL存储过程详解 mysql 存储过程mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored
- C#在执行此操作前必需先调用Bind方法的解决
- SQL 2005 中查询或执行另外的数据库操作的方法
- C#中检测某个类(方法、程序集等各种部分)是否应用了指定的特性以及对特性的一些简单操作
- C# 执行存储过程返回小数的输出值的设置方法
- c#定义一个方法,根据存储过程名称和存储过程参数数组,执行对应的存储过程
- 执行SQL语句时出现问题操作必须使用一个可更新的查询错误的解决方法
- C#中使用Char类中的方法对字符进行各种操作,判断是否为字母、数字、标点符号、分隔符或空白。
- SQL Server 第四堂课,创建存储过程。存储过程是一组编译在单个执行计划中的transact-SQL语句。存储过程相当于C#函数,可以允许模块化程序设计,允许更快执行如果某操作需要大量transct-SQL代码或需要重复执行,将在创建存储过程中对其进行分析和优化。
- Lua的系统学习(杂)_在VS中使用Lua_将C#方法注册进入Lua并执行的操作步骤