使用 SQLConnection 编译错误

Compile Errors Using SQLConnection

我的语法显示了这些错误:

The name 'command' does not exist
The name 'conn' does not exist

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

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 语句的末尾忘记了一个 ):

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())

您在 2 个地方忘记了右括号 )

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

using (SqlDataAdapter dataadapter1 = new SqlDataAdapter())