当我添加datatable类来填充datagridview时,Daily_Prices类没有显示为类。我希望textbox变成SQL命令,然后用查询结果填充datagridview。当我为DataTable类添加代码时,我无法使Daily_Prices类保留为数据层命名空间下的类
这是按钮单击事件触发command for text to SQL命令并使用查询结果填充datagridview的代码。它说明在命名空间DataLayer中没有类Daily_Prices。但是当您查看我的DataLayer名称空间时,会发现有一个Daily_Prices类。
private void buttonGo_Click(object sender, EventArgs e)
{
try
{
// Takes the commandBox text and sends it to SQL Server
DataLayer.Commands c = new DataLayer.Commands();
DataLayer.Daily_Prices dp = c.Command(string.Format(commandBox.Text));
// Populates the datagridview
DataTable daily = DataLayer.Commands.GetDaily("Daily_Price");
dataGridViewGetDaily.DataSource = daily;
}
catch { }
}DataLayer的名称空间如下所示
namespace DataLayer
{
public class Commands
{
public Daily_Prices Command(string commandBox)
{
Daily_Prices dp = new Daily_Prices();
using (SqlConnection connect = DB.GetSqlConnection())
{
using (SqlCommand cmd = connect.CreateCommand())
{
cmd.CommandText = @"{0}";
cmd.CommandText = string.Format(cmd.CommandText, commandBox.ToString());
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
dp.Load(reader);
}
return dp;
}
}
}
public static DataTable GetDaily(string commandBox)
{
DataTable table = new DataTable("Daily_Price");
SqlDataAdapter da = null;
using (SqlConnection conn = DB.GetSqlConnection())
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = @"{0}";
cmd.CommandText = string.Format(cmd.CommandText, commandBox.ToString());
da = new SqlDataAdapter(cmd);
da.Fill(table);
}
return table;
}
}
}发布于 2019-03-31 17:46:09
我在您的DataLayer-namespace中找不到任何Daily_Prices类的定义!
也许你误解了Daily_Prices dp = new Daily_Prices();这一行(它只是一个对象定义)?
我在DataLayer-namespace中看到的唯一类定义是Commands
https://stackoverflow.com/questions/55437015
复制相似问题