簡體   English   中英

使用ASPNETDB.MDF添加到c#asp.net中的表

[英]Add to a table in the c# asp.net using the ASPNETDB.MDF

有沒有一種方法可以訪問我在asp.net 4.0中創建新網頁時添加到默認ASPNETDB.MDF數據庫中的表。 我添加了一個包含5列的名為BuilderTable的表。 我還需要它自己增加ID。 我已經將is identity選項設置為yes,並且標識增量設置為1,因此如何在不向后面的代碼中的ID列發送值的情況下使其工作。 這是c#中的代碼:(我在switch語句中包含了它,所以不要介意情況1)

     case "1":
            if (Roles.IsUserInRole(usr.UserName, "Builder") == false)//if the user is not already in the builder role then add them
            {
                Roles.AddUserToRole(usr.UserName, "Builder");//here we add the user into the builder role

                string connection = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
                SqlConnection conn = null;
                conn = new SqlConnection(connection);
                conn.Open();

                using (SqlCommand cmd = new SqlCommand())
                {
                    int nothing = 0;
                    string query = String.Format("INSERT INTO 'BuilderTable' VALUES('{0}', '{1}', '{2}', '{3}', '{4}')", nothing, p.fName, p.lName, p.Address, usr.Email);
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = query;
                    cmd.ExecuteNonQuery();
                }
            }
            break;

此代碼導致錯誤:

'BuilderTable'附近的語法不正確

提前致謝!

編輯以為我應該澄清一下我已經在代碼的其他地方聲明了這個

      string userName = Request.QueryString["user"];
    MembershipUser usr = Membership.GetUser(userName);
    ProfileCommon p = Profile.GetProfile(usr.UserName);

您需要刪除表名周圍的單引號。 即“ BuilderTable”

(您應該使用參數化查詢。查找“ Sql注入攻擊”。)

您的身份錯誤是由於您試圖在身份字段中插入值。 您應該通過指定字段來插入值。

 INSERT INTO BLAH (Fields,,,,) VALUES(Values,,,,,)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM