无法从MS SQL表填充DataTable

时间:2016-12-15 18:34:47

标签: c# sql sql-server database

这是我到目前为止所做的:

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行,它应该返回几百行。我显然在这里遗漏了一些简单的东西?

2 个答案:

答案 0 :(得分:0)

        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。

答案 1 :(得分:0)

这应该适合你。

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 ! ");
            }
        }
    }
}