C# 将SQL数据绑定到datagrid
试图从SQL数据库中获取要在datagrid中显示的数据,但未显示该数据 代码如下:C# 将SQL数据绑定到datagrid,c#,sql-server,wpf,wpfdatagrid,C#,Sql Server,Wpf,Wpfdatagrid,试图从SQL数据库中获取要在datagrid中显示的数据,但未显示该数据 代码如下: using System; using System.Windows; using System.Data.SqlClient; namespace Data { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } string S
using System;
using System.Windows;
using System.Data.SqlClient;
namespace Data
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string SQLSERVER_ID = @""; - Removed the ID and Password intentionally to show this code.
string SQLDatabaseName = "Data Logging";
string SQLServerLoginName = "Data User";
string SQLServerPassword = "";
public void SQLCmd(string SQLString)
{
try
{
SqlConnection conDatabase = new SqlConnection(String.Format(@"Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", SQLSERVER_ID, SQLDatabaseName, SQLServerLoginName, SQLServerPassword));
conDatabase.Open();
SqlCommand cmdDatabase = new SqlCommand("INSERT INTO Bags (RecordNumber, Date, Time) VALUE ('1', '2014-08-01', '08:00:00')" + SQLString, conDatabase);
cmdDatabase.ExecuteNonQuery();
conDatabase.Close();
conDatabase.Dispose();
}
catch (Exception em)
{
MessageBox.Show(em.ToString());
}
}
public System.Data.DataTable GetDataTable(string SQLString)
{
try
{
System.Data.DataTable dtTable = new System.Data.DataTable("dtTable");
string ConnectionString = String.Format(@"Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", SQLSERVER_ID, SQLDatabaseName, SQLServerLoginName, SQLServerPassword);
SqlDataAdapter sqlAdpt = new SqlDataAdapter("SELECT * FROM bags ORDER BY RecordNumber, Date, Time", ConnectionString);
sqlAdpt.Fill(dtTable);
sqlAdpt.Dispose();
return dtTable;
}
catch (Exception em)
{
MessageBox.Show(em.ToString());
return new System.Data.DataTable();
}
}
向上编辑:sql查询中缺少结束引号
我有一个SQL数据库,里面有数据,但我不确定如何让datagrid显示它,因为这应该可以工作。除非我忘了补充什么。网格必须显示加载的数据
编辑:数据网格代码
<DataGrid Name="dtTable" Margin="0,64,0,0" AutoGenerateColumns="True" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" CanUserResizeColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="_Record Number" Binding="{Binding RecordNumber}"/>
<DataGridTextColumn Header="_Date" Binding="{Binding Date}"/>
<DataGridTextColumn Header="_Time" Binding="{Binding Time}"/>
</DataGrid.Columns>
</DataGrid>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="Bags_Data.Properties.Settings.BagsConnectionString"
connectionString="Data Source=*****;Initial Catalog="Bags";User ID=***;Password=*****"
providerName="System.Data.SqlClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
看起来您是在假设,因为DataGrid被称为dtTable,而System.Data.DataTable被称为dtTable,所以幕后的某些东西会为您将两者连接起来,或者它们以某种方式相互引用。事实并非如此。您需要在加载时实现调用GetDataTable函数的代码,将生成的DataTable设置为DataGrid的ItemSource属性。您还应该研究使用ViewModel进行绑定。我建议您要做的是重命名datagrid,以便更容易查看:
<DataGrid Name="RecordsDataGrid" Margin="0,64,0,0" AutoGenerateColumns="True" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" CanUserResizeColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="_Record Number" Binding="{Binding RecordNumber}"/>
<DataGridTextColumn Header="_Date" Binding="{Binding Date}"/>
<DataGridTextColumn Header="_Time" Binding="{Binding Time}"/>
</DataGrid.Columns>
</DataGrid>
我最终放弃了datagrid,而选择了listview,我很快就开始工作了,它在窗口加载时显示了来自SQL的数据 我补充说:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ShowData();
}
private void ShowData()
{
SqlConnection con = new SqlConnection(String.Format(@"Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", SQLSERVER_ID, SQLDatabaseName, SQLServerLoginName, SQLServerPassword));
con.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM Bags", con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
listView1.DataContext = dt.DefaultView;
}
已删除GetDataTable函数。您是否曾尝试在
.DataSource
属性中绑定此结果?您的DataGrid绑定代码在哪里编辑。我不相信它正在连接数据库,因为数据库中有记录。@zen_1991您应该在后面的代码中将数据绑定到datagrid。谢谢您尝试!
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ShowData();
}
private void ShowData()
{
SqlConnection con = new SqlConnection(String.Format(@"Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", SQLSERVER_ID, SQLDatabaseName, SQLServerLoginName, SQLServerPassword));
con.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM Bags", con);
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(comm);
da.Fill(dt);
listView1.DataContext = dt.DefaultView;
}