分享

asp.net使用GridView删除更新ACCESS数据库里的数据问题!急!!!

 行走在理想边缘 2019-02-15
一个网站先前用的是MS-SQL现在更换数据库 为ACCESS 

一个GridView里面有更新删除 因为语法有点不一样去做了修改 删除修改好了没什么问题..但是更新怎么也不行..老提示"语法错误 (操作符丢失) 在查询表达式 'where id="

就是那一堆单引号,和双银号闹的..大家帮我看看..谢谢啦!

数据库 字段
    id    txt    date
------------------------
 自动编号 备注   time
代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;

public partial class Admin_ad_jj : System.Web.UI.Page
{
    OleDbConnection con = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + " C:/Documents and Settings/AndersoN/桌面/xf/database/xf.mdb");
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        this.TextBox1.Text = "";
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
       
        con.Open();
        string ad_news = "insert into jj (txt)values('" + this.TextBox1.Text + "')";
        OleDbCommand com = new OleDbCommand(ad_news, con);
        com.ExecuteNonQuery();
        con.Close();
        bind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        con.Open();
        string sqlstr = "update jj set txt="+((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim()+"where id="+Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString())+"";
        
        OleDbCommand sqlcom = new OleDbCommand(sqlstr, con);
        sqlcom.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        con.Open();
        string sqlstr = "delete from jj where id=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "";
        OleDbCommand com = new OleDbCommand(sqlstr, con );
        com.ExecuteNonQuery();
        con.Close();
        bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    public void bind()
    {
        string sqlstr = "select * from jj";
        OleDbCommand myda = new OleDbCommand(sqlstr, con);
        con.Open();
        this.GridView1.DataSource = myda.ExecuteReader(); 
        GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();
        con.Close();
    }
}
在乐山,还在拿三四千工资?试试这个,收入提高好几倍! 广告 无极道 · 鹓鶵

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多