Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何查询SQL Server数据库?_C#_Sql Server_Linq To Sql - Fatal编程技术网

C# 如何查询SQL Server数据库?

C# 如何查询SQL Server数据库?,c#,sql-server,linq-to-sql,C#,Sql Server,Linq To Sql,我已经阅读了不错的教程,但当我尝试连接到SQL Server数据库时,它不起作用 我只需要做一个简单的查询,比如SELECT*fromtable,然后我需要处理该查询的结果值 这是我的代码,但是它没有将任何内容打印到控制台窗口中,我不知道为什么: namespace LinqConsoleApplication { [Table(Name = "Central")] public class Central {

我已经阅读了不错的教程,但当我尝试连接到SQL Server数据库时,它不起作用

我只需要做一个简单的查询,比如
SELECT*fromtable
,然后我需要处理该查询的结果值

这是我的代码,但是它没有将任何内容打印到控制台窗口中,我不知道为什么:

    namespace LinqConsoleApplication
    {
        [Table(Name = "Central")]
        public class Central
        {
            private string _UserID;
            [Column(IsPrimaryKey = true, Storage = "_UserID")]
            public string UserID
            {
                get
                {
                    return this._UserID;
                }
                set
                {
                    this._UserID = value;
                }

            }

        private string _Email;
        [Column(Storage = "_Email")]
        public string Email
        {
            get
            {
                return this._Email;
            }
            set
            {
                this._Email = value;
            }
        }

    } // end of class Central

    class Program
    {
        static void Main(string[] args)
        {

            // Use a connection string.
            DataContext db = new DataContext("Server=aaaaaaa; Database=bbbbbbbbb; uid=ccccccccc; pwd=ddddddddd; Trusted_Connection=True;");

            // Get a typed table to run queries.
            Table<Central> Centrals = db.GetTable<Central>();

            IQueryable<Central> custQuery =
                from central in Centrals
                select central;

            foreach (Central rowin custQuery)
            {
                Console.WriteLine("Email={0}", row.Email);
            }

            Console.ReadKey();

        }
    }

 } // end of namespace
名称空间LinqConsoleApplication
{
[表(Name=“Central”)]
中环公营
{
私有字符串_UserID;
[列(IsPrimaryKey=true,Storage=“\u UserID”)]
公共字符串用户ID
{
收到
{
返回此。\u UserID;
}
设置
{
这是。_UserID=value;
}
}
私人字符串\u电子邮件;
[列(存储=“\u电子邮件”)]
公共字符串电子邮件
{
收到
{
返回此邮件;
}
设置
{
这个。_Email=value;
}
}
}//课程结束中央
班级计划
{
静态void Main(字符串[]参数)
{
//使用连接字符串。
DataContext db=new DataContext(“服务器=aaaaaaa;数据库=BBBBB;uid=CCCCC;pwd=DDDDD;可信连接=True;”;
//获取类型化表以运行查询。
Table Centrals=db.GetTable();
可查询的客户查询=
从中央到中央
选择中心;
foreach(中央rowin custQuery)
{
WriteLine(“Email={0}”,row.Email);
}
Console.ReadKey();
}
}
}//名称空间的结尾

从Sql Server进行简单查询并将其填入DataTable,然后打印所有值:

private static void Main(string[] args)
{
  try
  {
    const string connString =
      "Data Source=127.0.0.1,1433\\sqlexpress;Initial Catalog=dbfirealarm;Integrated Security=false;Pooling=False;User ID=admin;Password=admin;Connection Timeout=5";
    using (var conn = new SqlConnection(connString))
    {
      using (var adapter = new SqlDataAdapter("SELECT * FROM TABLE_LOGGING", conn))
      {
        using (var dataTable = new DataTable())
        {
          adapter.Fill(dataTable);
          PrintDataTable(dataTable);
        }
      }
    }
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.ToString());
  }
}

private static void PrintDataTable(DataTable table)
{
  foreach (DataColumn column in table.Columns)
  {
    Console.Write("{0}\t", column.Caption);
  }
  Console.WriteLine();
  foreach (DataRow row in table.Rows)
  {
    foreach (var item in row.ItemArray)
    {
      Console.Write("{0}\t", item);
    }
    Console.WriteLine();
  }
}

表中有多少行?这一行更容易:这将更容易自己调试解决?你有没有逐级检查代码?检查你的连接字符串。它应该是UN:PWD或trusted connection,但不能同时是两者。您好,从您的代码来看,似乎有两种可能的情况:1。您的代码运行良好,这意味着您实际上进入了foreach循环,并且意味着您的表是空的。2.您的代码在早期某个点崩溃,您没有注意到,这意味着您的配置(连接字符串等)已关闭。您可以通过作为liamsuggsested单步执行,或者在for循环之后添加console write来确定哪一个是正确的,以查看您的代码是否真正到达了那里。