【问题标题】:Trouble filling a DataTable from MS SQL table从 MS SQL 表填充数据表时遇到问题
【发布时间】:2017-05-01 11:08:00
【问题描述】:

这是我目前所拥有的:

static void Main(string[] args)
        {
            DataTable t = new DataTable();

            string connetionString = null;
            SqlConnection cnn ;
            connetionString = "Data Source=local.url;Initial Catalog=databasename;User ID=username;Password=password";

            cnn = new SqlConnection(connetionString);

            string sql = "SELECT * FROM shiplabels";
            SqlDataAdapter a = new SqlDataAdapter(sql, cnn);

            try
            {
                cnn.Open();
                a.Fill(t);
                cnn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine ("Can not open connection ! ");
            }
        }

我想连接到这个 Microsoft DB 并从中提取数据。我很难让它工作!当我使用此代码时,数据表 t 有 0 行,它应该返回几百行。我显然在这里遗漏了一些简单的东西?

【问题讨论】:

标签: c# sql sql-server database


【解决方案1】:

这应该适合你。

using System;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection sqlCnn ;
            SqlCommand sqlCmd ;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            string sql = null;

            connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
            sql = "Select * from product";

            sqlCnn = new SqlConnection(connetionString);
            try
            {
                sqlCnn.Open();
                sqlCmd = new SqlCommand(sql, sqlCnn);
                adapter.SelectCommand = sqlCmd;
                adapter.Fill(ds);
                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]);
                }
                adapter.Dispose();
                sqlCmd.Dispose();
                sqlCnn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}

【讨论】:

    【解决方案2】:
            DataTable dt = new DataTable();
            SqlDataAdapter sqlAdtp = new SqlDataAdapter();
            string connectionString = "Data Source=local.url;Initial Catalog=databasename;User ID=username;Password=password";
            string sql = "SELECT * FROM shiplabels";
    
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.CommandType = CommandType.Text;                   
    
                    try
                    {
                        sqlAdtp.SelectCommand = cmd;
                        sqlAdtp.Fill(dt);
                    }
                    catch (Exception ex)
                    {
    
                    }
                }
            }
    

    首先,使用SqlDataAdapter时不需要打开连接。 另外,您忘记了 CommandType。

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 2015-08-02
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 2013-02-18
      • 2011-05-22
      相关资源
      最近更新 更多