使用ADO为控制台生成输出 [英] Produce Output for console using ADO

查看:75
本文介绍了使用ADO为控制台生成输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
下面是我从存储过程中获取数据的代码,但我在此行上不断出错

SqlDataReader reader = command.ExecuteReader();

Hi all
Below is my code to get data from a stored procedure but i keep getting error on this line

SqlDataReader reader = command.ExecuteReader();

<pre>
view source
print?
01	using System;
02	using System.Collections.Generic;
03	using System.Data;
04	using System.Data.SqlClient;
05	using System.Configuration;
06	using System.Text;
07	 
08	 
09	namespace ExecuteStoredProcedure
10	{
11	    class Program
12	    {
13	        private int _dateID;
14	 
15	        public Program(int dateID) {
16	            _dateID = dateID;
17	        }
18	 
19	       public int dateID { get; set; }
20	 
21	        public void LoadData(int p)
22	        {
23	            
24	            SqlConnection conn = new SqlConnection("SOMEDATABASE");
25	            SqlCommand command = new SqlCommand("p_Date_sel", conn);
26	            SqlParameter parameter = new SqlParameter();
27	            parameter.ParameterName = "@DateID";
28	             
29	            command.CommandType = CommandType.StoredProcedure;
30	            command.Parameters.Add(parameter);
31	 
32	            conn.Open();
33	            SqlDataReader reader = command.ExecuteReader();
34	            while (reader.Read())
35	            {
36	                Console.WriteLine(reader["Dates"]);
37	            }
38	            conn.Close();
39	 
40	           }
41	    }
42	}



如果您感兴趣的话,这里是主班.

查看源代码



Here''s a main class if your intrested

view source

01	using System;
02	using System.Collections.Generic;
03	using System.Linq;
04	using System.Text;
05	 
06	namespace ExecuteStoredProcedure
07	{
08	    class TestClass
09	    {
10	        public static void Main(string[] args)
11	        {
12	            Program p = new Program(56);
13	            p.LoadData(56);
14	            Console.WriteLine("DateID = " + "" + p.dateID);
15	            Console.ReadLine();
16	        }
17	    }
18	}



请告诉我我做错了什么.我是一个仍在接受培训的新开发人员,下一步是创建insert,update和delete方法.如果您愿意教我学习,在此先感谢



Please tell me what i''m doing wrong.I''m a new developer still under training, the next step for me would be creating a insert,update and delete method.If your willing to teach i''m willing to learn,thanks in advance

推荐答案




http://www.akadia.com/services/dotnet_data_reader.html [ http://csharp.net-informations.com/ado.net/ado.net-data-access-architecture.htm [ ^ ]

此链接对您有用.

谢谢;)
Hi,


http://www.akadia.com/services/dotnet_data_reader.html[^]


For More Learn

http://csharp.net-informations.com/ado.net/ado.net-data-access-architecture.htm[^]

This links are useful to you.

Thanks ;)


我找不到在
中设置参数值的代码 public void LoadData(int p)


I can''t find code where you set the value of the parameter in the
public void LoadData(int p)


SqlConnection conn = new SqlConnection("SOMEDATABASE");
SqlCommand command = new SqlCommand("p_Date_sel", conn);
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@DateID";


//You are missing following line.
<big>parameter.Value = p;</big>

command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(parameter);

conn.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    Console.WriteLine(reader["Dates"]);
}
conn.Close();


大家好,

谢谢您的帮助.
现在我在日期上出现错误,这对我来说是正确的方法吗,计划仅从dateID中获取所有参数

Hi All,

Thank you for helping.
Now i''m getting an error on dates,is this the correct way for me to do this,Planning on getting all the parameters out just from the dateID

class Program
    {
        private int _dateID;
        private int _dateTypeID;
        private DateTime _date;
        private string _name;
        private string _notes;


        public Program(int dateID) {
            _dateID = dateID;
        }
        public Program(int datetypeID,DateTime date,string name,string notes){
        
            _dateTypeID = datetypeID;
            _date = date;
            _name = name;
            _notes = notes;
        }

        

       public int dateID { get; set; }
       public int dateTypeID { get; set; }
       public DateTime date { get; set; }
       public string name { get; set; }
       public string notes { get; set; }
       //suppose to retrieve all the data from DateID and show all
       // the attributes in table Date
       //Am i doing this the right way?


        
        public void LoadData(int p)
        {
           
            SqlConnection conn = new SqlConnection("Data Source=mycbj01psql03\\sandbox01;Initial Catalog=DBRMS;Integrated Security=True");
            SqlCommand command = new SqlCommand("p_Date_sel", conn);
            SqlParameter parameter = new SqlParameter();
            parameter.ParameterName = "@DateID";
            parameter.Value = p;// just added this 

            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "p_Date_sel";
            
            command.Parameters.Add(parameter);


            conn.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["Dates"]); //error on this line 
            }
            conn.Close();

           }
    }





public static void Main(string[] args)
        {
            /*No 56 is already in the table with all the parameters,
              i cant even show the dateID*/
            Program p = new Program(56); 
            p.LoadData(56);
            
            Console.WriteLine("DateID = " + "" + p.dateID);
            Console.WriteLine("DateTypeID = " + "" + p.dateTypeID);
            Console.WriteLine("Date = " + "" + p.date);
            Console.WriteLine("Name = " + "" + p.name);
            Console.WriteLine("Notes = " + "" + p.notes);
            
            Console.ReadLine();
        }


这篇关于使用ADO为控制台生成输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆