C# 未找到SqlDataAdapter类型或命名空间

C# 未找到SqlDataAdapter类型或命名空间,c#,sql,sql-server,uwp,C#,Sql,Sql Server,Uwp,我想从我的UWP应用程序连接我的SQL Server数据库,但我一直收到一个错误,即未找到SqlDataAdapter类型或命名空间,并且我的datagrid没有源。我添加了使用System.Data.SqlClient和使用System.Data语句 string cs = "Data Source=.;Initial Catalog=Artist;Integrated Security=True"; SqlConnection con; SqlDataAdapter adapt; Data

我想从我的UWP应用程序连接我的SQL Server数据库,但我一直收到一个错误,即未找到
SqlDataAdapter
类型或命名空间,并且我的datagrid没有源。我添加了
使用System.Data.SqlClient
使用System.Data语句

string cs = "Data Source=.;Initial Catalog=Artist;Integrated Security=True";

SqlConnection con;
SqlDataAdapter adapt;
DataTable dt;

//frmSearch Load Event   
private void frmSearch_Load(object sender, EventArgs e)
{
    con = new SqlConnection(cs);
    con.Open();

    adapt = new SqlDataAdapter("select * from ArtistTable", con);
    dt = new DataTable();
    adapt.Fill(dt);

    ArtistGV.DataSource = dt;
    con.Close();
}

private void txtsearch_TextChanged(object sender, TextChangedEventArgs e)
{
    con = new SqlConnection(cs);
    con.Open();

    adapt = new SqlDataAdapter("select * from ArtistTable where ArtistName like '" + txtsearch.Text + "%'", con);
    dt = new DataTable();
    adapt.Fill(dt);

    ArtistGV.DataSource = dt;
    con.Close();
}

您的应用程序的目标版本和最低版本必须至少为Fall Creators Update(16299),才能使用
System.Data.SqlClient
命名空间。你可以查一下电话号码。您可以在项目的属性中更改目标(右键单击解决方案资源管理器中的项目节点)

另外,如果
ArtistGV
GridView
,则需要使用
ItemsSource
属性,而不是
DataSource
。如果希望创建表格数据网格,请使用
这个它与WPF和ASP.NET中的
DataGrid
更为匹配。

请确保在项目中添加对System.Data的引用,只是在SqlDataadapter和ArtistGV.DataSource上,我得到了一个错误-您不应该将SQL语句连接在一起-使用参数化查询来避免SQL注入-查看不相关的提示:
SqlConnection
SqlDataadapter
都是
IDisposable
的,因此每个都应该在一个
使用
块。完成后,就不需要关闭,因为隐式Dispose在退出using块时会调用它。谢谢。部分解决了我的问题。我现在只在ArtistGV.Datasource=dt上有一个错误;线它说Gridview不包含DatSource的定义。我已经更新了我的答案,您可能希望使用Windows社区工具包中的
DataGrid
控件。谢谢。解决了这个问题。你知道为什么我的数据没有显示吗?你使用的是
DataGrid
?要在
GridView
中显示,您必须首先创建
ItemTemplate
,否则您将只得到一个简单的
类型.ToString()
内容。可能最好在解决此问题后关闭此问题,并为此创建一个新问题,因为它现在是一个不同的问题。谢谢,我会这样做。对不起,我对这一切还是新手。