使用 SQLConnection 编译错误

Posted

技术标签:

【中文标题】使用 SQLConnection 编译错误【英文标题】:Compile Errors Using 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 - 纠正了我目前的两个错误,但产生了其他错误。 那么问题可能不止一个! 【参考方案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);我现在得到一个语法错误的编译错误,','预期

以上是关于使用 SQLConnection 编译错误的主要内容,如果未能解决你的问题,请参考以下文章

实体框架 6 错误服务器版本:(System.Data.SqlClient.SqlConnection)customerOrderContext.Database.Connection).Server

SqlConnection 卡在命名管道上

flash里的编译器错误是啥意思啊?

错误:使用 cargo 编译时无法编译 `time`

使用 ARM GCC 编译列表迭代器时的模板编译时错误

使用带有类错误的地图,编译错误