【问题标题】:Issue with using and ODBC/MySql使用和 ODBC/MySql 的问题
【发布时间】:2012-05-04 19:10:43
【问题描述】:

这是我尝试在 C# 中使用 using(用于连接池)的示例代码。现在我认为由于不在方法中,它给了我一个错误。但是,有没有办法解决这个问题?

<% @Page Language="C#" %>
<% @Import Namespace="System.Data.Odbc" %>
<% @Import Namespace="System.Web.Configuration" %>

<script language="C#" runat="server">
string conString = WebConfigurationManager.ConnectionStrings["cheese"].ConnectionString;
using (OdbcConnection con = new OdbcConnection(conString)) {
    con.Open();
    using (OdbcCommand com = new OdbcCommand("SELECT pies FROM ducks WHERE isapie = nope", con)) {
        com.Parameters.AddWithValue("@var", paramWord);
        using (OdbcDataReader reader = com.ExecuteReader()) {
            while (reader.Read()) {
                Response.Write(reader.GetString(0));
            }
        }
    }
    con.Close();
}
</script>

现在给出错误的行是:

Line 8:     using (OdbcConnection con = new OdbcConnection(conString)) {

而有问题的错误是:

Compiler Error Message: CS1519: Invalid token 'using' in class, struct, or interface member declaration

我试图让我的代码保持简约、易于编辑等,因此我想避免仅仅为了拥有它们而使用不必要的类、方法等。

【问题讨论】:

  • 首先,您需要将con.Open(); 之后的右大括号向下移动到con.Close(); 之后。在完成连接之前,您正在处理连接。
  • 完成并编辑。但是在同一行出现同样的错误。

标签: c# asp.net mysql odbc connection-pooling


【解决方案1】:

您有几个问题;第一个是您没有在方法中定义代码。为此,您需要将其放在 Page_Load 中,例如:

<script language="C#" runat="server">

void Page_Load(object sender,EventArgs e)
{
    string conString = WebConfigurationManager.ConnectionStrings["cheese"].ConnectionString;
    using (OdbcConnection con = new OdbcConnection(conString)) {
    con.Open();

    using (OdbcCommand com = new OdbcCommand("SELECT pies FROM ducks WHERE isapie = nope", con)) {
         com.Parameters.AddWithValue("@var", paramWord);
         using (OdbcDataReader reader = com.ExecuteReader()) {
          while (reader.Read()) 
             Response.Write(reader.GetString(0));
         }//end using reader
     }//end using ODBCCommand
  }//end using ODBCConnection
}//page_load
</script>

第二个问题是你使用using打开连接,你尝试使用关闭}下面的连接对象。当您到达实例化 OdbcCommand 的第二个 using 时,连接已被释放。你需要做这样的事情:

void Page_Load(object sender,EventArgs e)
{
string conString = WebConfigurationManager.ConnectionStrings["cheese"].ConnectionString;
using (OdbcConnection con = new OdbcConnection(conString)) {
    con.Open();

using (OdbcCommand com = new OdbcCommand("SELECT pies FROM ducks WHERE isapie = nope", con)) {
    com.Parameters.AddWithValue("@var", paramWord);
    using (OdbcDataReader reader = com.ExecuteReader()) {
        while (reader.Read()) {
            Response.Write(reader.GetString(0));
        }
    }
 }
 con.Close();
 }
}

最后,您应该使用 gridview 或类似的东西来绑定这些数据,而不是像您那样写到Response Stream。假设您的页面中有一个 Gridview;你可以完美地做到这一点:

gridView.DataSource=com.ExecuteReader();
gridView.DataBind();

【讨论】:

  • 啊,我不太愿意让 .Net 为我生成代码...做漂亮的 JavaScript 事情(这更像是我的专业领域)。不过谢谢你,你的解释很有帮助,而且你很有礼貌!我好几年没学过C#了,有些人在我提问的时候很粗鲁。
猜你喜欢
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多