如何从C#查询Sql表 [英] How Do I Query Sql Tables From C#

查看:88
本文介绍了如何从C#查询Sql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



很抱歉,如果这是一个非常愚蠢的问题,但我是c#的新手,我还没有为初学者找到任何体面的教程。



我要做的是读写现有的SQL数据库。



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Data.SqlClient;

命名空间 EQCasTest
{
class Program
{
静态 void Main( string [] args)
{
SqlConnection myConnection = new SqlConnection( user id = username; +
@ password = password; server = test-pc\sqlexpress; +
Trusted_Connection = yes; +
database = eqcas; +
connection timeout = 10);
尝试
{
myConnection.Open();
}
catch (例外e)
{
Console.WriteLine(e.ToString());
}
尝试
{
SqlDataReader myReader = null ;
SqlCommand myCommand = new SqlCommand( select *来自cas_user_ext
myConnection);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine(myReader [ fullname]。ToString());
Console.WriteLine(myReader [ email]。ToString());
Console.WriteLine(myReader [ x_id]。ToString());
myReader.NextResult();
}
}
catch (例外e)
{
Console.WriteLine(e.ToString( ));
}
}
}
}





以上代码是我的代码i用于建立连接,当它从数据库中读取时,我只收到一行数据,这将是



John Rambo

john.rambo@ispowerfull.com

501



现在我希望它返回所有fullnameemail和x_id数据给我而不是只有一行。而且当我想阅读特定fullanme用户的所有列时,我该怎么做?



非常感谢任何帮助。

解决方案

注释掉这一行并再试一次。



 myReader.NextResult(); 


NextResult()返回 true 如果有更多结果集;否则它将返回 false



在这种情况下,查询只返回一个记录集并打破循环。



试试这个:

 (myReader.Read())
{
Console.WriteLine(Convert.ToString(myReader [ fullname])); // Convert.ToString()处理空值
Console.WriteLine(Convert.ToString(myReader [ email]));
Console.WriteLine(Convert.ToString(myReader [ x_id]));
}







- Amy


As Syed Asif Iqbal说删除

 myReader.NextResult(); 



删除后你可以一次获得所有记录。



第二个问题,阅读特定全名的所有栏目,你可以写linq:



 DataTable dt = new DataTable(); 
dt.Load(myReader);
var result =(来自m in dt.AsEnumerable()
其中m.Field < string > (fullname)==Name
select m).ToList();


Hi all,

Sorry if this is a really stupid question but i'm new to c# and i have yet to find any decent tutorials for beginners.

What i'm trying to do is read and write to an existing SQL database.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace EQCasTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection myConnection = new SqlConnection("user id=username;" +
                                           @"password=password;server=test-pc\sqlexpress;" +
                                           "Trusted_Connection=yes;" +
                                           "database=eqcas; " +
                                           "connection timeout=10");
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            try
            {
                SqlDataReader myReader = null;
                SqlCommand myCommand = new SqlCommand("select * from cas_user_ext",
                                                         myConnection);
                myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    Console.WriteLine(myReader["fullname"].ToString());
                    Console.WriteLine(myReader["email"].ToString());
                    Console.WriteLine(myReader["x_id"].ToString());
                    myReader.NextResult();
                } 
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}



This above code is my code i'm using to establish the connection and when it reads from the database i am only receiving one row of data which would be

John Rambo
john.rambo@ispowerfull.com
501

Now i want it to return all "fullname" "email" and "x_id" data to me instead of just 1 row. And also when i want to read all the columns for a specific "fullanme" user how would i do that?

Would appreciate any assistance.

解决方案

Comment out this line and try again.

myReader.NextResult();


NextResult() returns true if there are more result sets; otherwise it'll return false.

In this case, query is returning only one record set and breaking the loop.

Try this:

while (myReader.Read())
{
    Console.WriteLine(Convert.ToString(myReader["fullname"]));//Convert.ToString() handles null values
    Console.WriteLine(Convert.ToString(myReader["email"]));
    Console.WriteLine(Convert.ToString(myReader["x_id"]));
}




--Amy


As Syed Asif Iqbal said remove

myReader.NextResult();


after removing this you can get all record at a time.

For your second question, read all the columns for a specific "fullname" you can write linq:

DataTable dt = new DataTable();
              dt.Load(myReader );
              var result = (from m in dt.AsEnumerable()
                             where m.Field<string>("fullname") == "Name"
                             select m).ToList();


这篇关于如何从C#查询Sql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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