【问题标题】:Compile Errors Using SQLConnection使用 SQLConnection 编译错误
【发布时间】:2016-11-13 15:38:10
【问题描述】:

我的语法显示这些错误:

名称“命令”不存在
名称“conn”不存在

我声明了这两个变量,为什么会出现错误?这是完整的语法。

namespace SQLDataPull
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SQL.DataTable dtData = new SQL.DataTable();
        string conString = @"Server=ProdDev;Database=Test;Integrated Security=SSPI;";
        StringBuilder query = new StringBuilder();
        SQL.DataTable dtProducts = new SQL.DataTable();
        query.Append("SELECT Top 1 [saleID] FROM [dbo].[saleorderitems] ORDER BY [saleID] ASC");
        //Populating datatable1 with the saleID
        using (SqlConnection cn = new SqlConnection(conString))
        {
            using (SqlDataAdapter da = new SqlDataAdapter(query.ToString(), cn))
                da.Fill(dtProducts);
        }
        //Iterating the saleid from datatable
        foreach (DataRow row in dtProducts.Rows)
        {
            using (SqlConnection conn = new SqlConnection("Server=ProdDev;Database=Test;Integrated Security=SSPI;")
            {
                SqlCommand command = new SqlCommand();
                command.CommandText = "SELECT * FROM [dbo].[master] WHERE saleID = @saleID;";
                command.Parameters.Add("@saleID", SqlDbType.VarChar);
                command.Parameters["@saleID"].Value = row.Field<string>("saleID");
                command.Connection = conn;                    
                using (SqlDataAdapter dataadapter1 = new SqlDataAdapter()
                {
                    dataadapter1.Fill(dtData);
                }
            }
        }  
    }
}
}

【问题讨论】:

  • 哪一行出现错误?
  • using (SqlConnection conn 缺少右括号
  • @Plutonix - 纠正了我目前的两个错误,但产生了其他错误。
  • 那么问题可能不止一个!

标签: c# .net datatable sqlcommand sqldataadapter


【解决方案1】:

您在 2 个位置忘记了右括号 )

using (SqlConnection conn = 
       new SqlConnection("Server=ProdDev;Database=Test;Integrated Security=SSPI;"))

using (SqlDataAdapter dataadapter1 = new SqlDataAdapter())

【讨论】:

    【解决方案2】:

    您在 using 语句的末尾忘记了一个 )

    using (SqlConnection conn = new 
                 SqlConnection("Server=ProdDev;Database=Test;Integrated Security=SSPI;")
    

    using (SqlConnection conn = new 
                  SqlConnection("Server=ProdDev;Database=Test;Integrated Security=SSPI;"))
    

    您还需要为:

    using (SqlDataAdapter dataadapter1 = new SqlDataAdapter())
    

    【讨论】:

    • 这一行dataadapter1.Fill(dtData);我现在得到一个语法错误的编译错误,','预期
    猜你喜欢
    • 2011-09-10
    • 2011-07-13
    • 2012-08-26
    • 2016-08-23
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多