我的代码在da.fill(dt)中显示错误; [英] my code is showing error in da.fill(dt);

查看:143
本文介绍了我的代码在da.fill(dt)中显示错误;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码显示错误

da.fill(dt);



请找出错误并给我解决方案< br $>




'3'附近的语法不正确。

描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



异常详细信息:System.Data.SqlClient.SqlException:'3'附近的语法不正确。



来源错误:





第29行:dt = new DataTable();

第30行:da = new SqlDataAdapter(cmd);

第31行:da.Fill(dt);

第32行:return dt;

第33行:



源文件:g:\ WebSite \WebSite1 \ App_Code\bal.cs行:31











我的代码在这里







这是我的bal



使用系统;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;

使用System.Data;

使用System.Data.SqlClient;

使用System.Configuration;



/ //< summary>

/// b的摘要说明al $ / $
///

公共类bal

{

SqlCommand cmd;

SqlConnection con;

SqlDataAdapter da;

DataTable dt;

public bal()

{



con = new SqlConnection(ConfigurationManager.ConnectionStrings [dbconnection]。ToString());



} < br $>


public DataTable select()

{



cmd = new SqlCommand( select * from 3tier,con);

dt = new DataTable();

da = new SqlDataAdapter(cmd);

da .Fill(dt);

返回dt;



}

public void insert(dal dl)

{

cmd =新的SqlCommand(插入3层(名称,城市,年龄)值(@ name,@ city,@ age),con);



cmd.P arameters.AddWithValue(@ name,dl.name);



cmd.Parameters.AddWithValue(@ city,dl.city);



cmd.Parameters.AddWithValue(@ age,dl.age);





con.Open();

cmd.ExecuteNonQuery();

con.Close();



$ / b $ b $

}





我的dal代码
使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;



使用System.Data;



///< summary>

/// dal的摘要说明

///

公共类dal

{



public int Id {get;组; }

公共字符串名称{get;组; }

public string city {get;组; }

public int age {get;组; }









bal bl = new bal();

public DataTable select1()

{

返回bl.select();





}

public void insert(dal dl)

{

bl.insert(dl);



}

}





我的UI代码





使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web;



使用System.Data;



///< summary>

/// dal的摘要说明

///

public class dal

{



public int Id {get;组; }

公共字符串名称{get;组; }

public string city {get;组; }

public int age {get;组; }









bal bl = new bal();

public DataTable select1()

{

返回bl.select();





}

public void insert(dal dl)

{

bl.insert(dl);



}

}

我的aspx页面





my code is showing error in
da.fill(dt);

please find the error and give me the solution


Incorrect syntax near '3'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '3'.

Source Error:


Line 29: dt = new DataTable();
Line 30: da = new SqlDataAdapter(cmd);
Line 31: da.Fill(dt);
Line 32: return dt;
Line 33:

Source File: g:\WebSite\WebSite1\App_Code\bal.cs Line: 31





my code is here



this is my bal

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for bal
///
public class bal
{
SqlCommand cmd;
SqlConnection con;
SqlDataAdapter da;
DataTable dt;
public bal()
{

con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());

}

public DataTable select()
{

cmd = new SqlCommand("select * from 3tier", con);
dt = new DataTable();
da = new SqlDataAdapter(cmd);
da.Fill(dt);
return dt;

}
public void insert(dal dl)
{
cmd = new SqlCommand("insert into 3tier(name,city,age)values(@name,@city,@age)", con);

cmd.Parameters.AddWithValue("@name", dl.name);

cmd.Parameters.AddWithValue("@city", dl.city);

cmd.Parameters.AddWithValue("@age", dl.age);


con.Open();
cmd.ExecuteNonQuery();
con.Close();


}
}


my dal code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;

/// <summary>
/// Summary description for dal
///
public class dal
{

public int Id { get; set; }
public string name { get; set; }
public string city { get; set; }
public int age { get; set; }




bal bl = new bal();
public DataTable select1()
{
return bl.select();


}
public void insert(dal dl)
{
bl.insert (dl);

}
}


my UI code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;

/// <summary>
/// Summary description for dal
///
public class dal
{

public int Id { get; set; }
public string name { get; set; }
public string city { get; set; }
public int age { get; set; }




bal bl = new bal();
public DataTable select1()
{
return bl.select();


}
public void insert(dal dl)
{
bl.insert (dl);

}
}
my aspx page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="register.aspx.cs" Inherits="register" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 91px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="style1">
        <tr>
            <td align="right" class="style2">
                Id</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                name</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                city</td>
            <td>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                age</td>
            <td>
                <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="insert" />
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td align="right" class="style2">
                &nbsp;</td>
            <td>
                <asp:GridView ID="GridView1" runat="server">
                </asp:GridView>
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>

    </div>
    </form>
</body>
</html>









我的网络配置文件







my web config file

<configuration>
    <connectionStrings>
        <add name="dbconnection" connectionString="Data Source=SUBHAN-PC;Integrated Security=true;Initial Catalog=subhan" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    </system.web>
</configuration>

推荐答案

这是表名:

It's the table name:
cmd = new SqlCommand("insert into 3tier(name,city,age)values(@name,@city,@age)", con);

SQL看到'3'并假设它是一个数字,所以其余的tablename deosn ; t适合任何合适的语法。

尝试转义表名:

SQL sees the '3' and assumes it's a number, so the rest of the tablename deosn;t fit any suitable syntax.
Try escaping the table name:

cmd = new SqlCommand("insert into [3tier] (name,city,age)values(@name,@city,@age)", con);


更正你的sqlqueries:



写下如下表格



[tablename]





在任何表名或任何其他方法的启动时都不使用数值,这是一个糟糕的编程习惯
correct your sqlqueries:

by writing table name like below

[tablename]


dont use numeric values at starting of any table name, or any other methods,procedures its a bad programming practice


这篇关于我的代码在da.fill(dt)中显示错误;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆