English 中文(简体)
议题与使用和处置BC/MySql
原标题:Issue with using and ODBC/MySql

因此,这是我关于在C#使用(联线集合)的尝试的样本代码。 现在,我认为这给我留下了一个错误,因为这不是一种方法。 然而,是否有办法解决这一问题?

<% @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

I am trying to keep my code minimalistic, easy to edit, etc. and so I d like to avoid having unnecessary classes, methods, etc. simply for the sake of having them.

最佳回答

你有几个问题;第一个问题是,你没有以某种方法界定你的法典。 为此,您必须将其放在第_页内。 例如:

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

The second problem is that you use using to open the connection and you attempt to use the connection object below the closing }. By the the time you reach the second using where you instantiate your OdbcCommand the connection is already disposed. you need to do something like this instead:

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.DataSource=com.ExecuteReader();
gridView.DataBind();
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签