這是我出現的問題:

這是我的代碼:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace 資料控制元件高級應用
{
public partial class 資料控制元件高級應用 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) //驗證頁面是否回發
{
GridViewBind();//呼叫系結資料方法
}
}
public void GridViewBind()
{
using (SqlConnection sqlCon = new SqlConnection())
{
sqlCon.ConnectionString = "server=DESKTOP-VJO357M\\ME2020;uid=sa;pwd=19980608;database=電影庫";
string SqlStr = "select * from 電影資料"; //定義SQL陳述句
sqlCon.Open(); //打開資料庫連接
SqlDataAdapter da = new SqlDataAdapter(SqlStr, sqlCon);//實體化SqlDataAdapter物件
DataSet ds = new DataSet(); //實體化資料集DataSet
da.Fill(ds); //將資料填充到DataSet
GridView1.DataSource = ds; //系結DataList控制元件
GridView1.DataBind(); //執行系結
da.Dispose(); //釋放資源
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;//設定控制元件新的頁碼
GridViewBind(); //重新系結資料
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
//設定GridView控制元件的編輯項的索引為選擇的當前索引
GridView1.EditIndex = e.NewEditIndex;
//資料系結
GridViewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//取得編輯行的關鍵欄位的值
string 電影名 = GridView1.DataKeys[e.RowIndex].Value.ToString();
//取得文本框中輸入的內容
string 累計票房 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
string 導演 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string 主演 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
string 上映地區 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
string 國家及地區 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
string 發行公司 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
string 型別 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
//定義SQL陳述句
string sqlStr = "update 電影資料 set 累計票房='" + 累計票房 + "',導演='" + 導演 + "',主演='" + 主演 + "',上映地區='" + 上映地區 + "',國家及地區='" + 國家及地區 + "',發行公司='" + 發行公司 + "',型別='" + 型別 + "' where 電影名=" + 電影名;
SqlConnection myConn = new SqlConnection("server=DESKTOP-VJO357M\\ME2020;uid=sa;pwd=19980608;database=電影庫");
myConn.Open();
//執行SQL陳述句
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
//設定編輯行的索引
GridView1.EditIndex = -1;
//重新系結
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
//設定GridView控制元件的編輯項的索引為-1,即取消編輯
GridView1.EditIndex = -1;
//資料系結
GridViewBind();
}
}
}
uj5u.com熱心網友回復:
已經提示了,SQL陳述句中,括號可能用了中文括號,或有多余,請檢查uj5u.com熱心網友回復:
你用中文做變數名沒問題嗎看著腦殼疼,你把sqlStr跟出來放到資料庫看能不能執行uj5u.com熱心網友回復:
有問題的不是代碼,而是那行ExecuteNonQuery內包含的Sql查詢陳述句,或者SqlConnection字串uj5u.com熱心網友回復:
打個斷點在你的Update SQL陳述句上,看看拼接的sql陳述句是什么,應該是sql陳述句有錯誤uj5u.com熱心網友回復:
中文欄位前要加Nuj5u.com熱心網友回復:
就是拼接SQL出問題了uj5u.com熱心網友回復:
看的ID我就是笑著進來的,其實我想早點睡覺,可是老出BUG,睡不著。欄位名稱加[]試試,[電影名稱],看看有無中文單引號或者標點符號。把SQL陳述句拿到資料庫執行,能不能運行?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/43112.html
標籤:ASP.NET
上一篇:請問一下這種sql該怎么寫
下一篇:在執行洗掉操作時出現“System.Data.OleDb.OleDbException:“找不到可安裝的 ISAM。的問題,求大佬指點迷津,在線等,挺急的”
