首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在尝试将文本框中的文本转换为SQL命令,然后使用查询结果填充datagridview

我正在尝试将文本框中的文本转换为SQL命令,然后使用查询结果填充datagridview
EN

Stack Overflow用户
提问于 2019-03-31 09:09:37
回答 1查看 26关注 0票数 0

当我添加datatable类来填充datagridview时,Daily_Prices类没有显示为类。我希望textbox变成SQL命令,然后用查询结果填充datagridview。当我为DataTable类添加代码时,我无法使Daily_Prices类保留为数据层命名空间下的类

这是按钮单击事件触发command for text to SQL命令并使用查询结果填充datagridview的代码。它说明在命名空间DataLayer中没有类Daily_Prices。但是当您查看我的DataLayer名称空间时,会发现有一个Daily_Prices类。

代码语言:javascript
复制
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的名称空间如下所示

代码语言:javascript
复制
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;
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-03-31 17:46:09

我在您的DataLayer-namespace中找不到任何Daily_Prices类的定义!

也许你误解了Daily_Prices dp = new Daily_Prices();这一行(它只是一个对象定义)?

我在DataLayer-namespace中看到的唯一类定义是Commands

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55437015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档