为什么我的C#' System.data.sqlclient'工作不正常?

时间:2016-09-02 19:09:23

标签: c# sql sql-server visual-studio

我正在创建一个Windows 10通用应用程序并需要数据表'功能,但是当我创建一个DataTable对象

DataTable test = new DataTable();

发生错误

  

DataTable不包含带0参数的构造函数

另外,我已经使用子句添加了命名空间:

using System.Data;
using System.Data.SqlClient;

我想知道是否有人知道如何解决这个错误?

此外,在查看intellisense时,我看不到SqlDataAdapter功能,但是,我可以看到SqlDataReader

我想知道是否有人对这些问题有任何见解?

我正在运行Microsoft Visual Studio 2015 Enterprise并安装了Microsoft SQL Server Management Studio。

修改

我没有一个名为DataTable的课程,我尝试了John Wu所说的并创建了' var'测试变量,我仍然收到相同的错误。

以下是我的页面的完整代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SQLite.Net;
using System.Data.SqlClient;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using System.Data;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace PeriodicTableWin10
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        string ConnectionString = "Connection string goes here";

        private void ShowElement(string ElementName)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                try
                {
                    SqlCommand GetElement = new SqlCommand("Select ElementName from tblElement WHERE ElementName=@Element");
                    GetElement.Parameters.Add(new SqlParameter("@Element", ElementName));

                    DataTable test = new DataTable();
                }
                catch(Exception e)
                {
                    e.ToString();
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

更改

DataTable test = new DataTable()

var test = new System.Data.DataTable()

确保您实例化正确的班级。

相关问题