标签文本保存到数据库

时间:2014-09-08 08:18:44

标签: c# mysql asp.net


我想创建一个标签,标签名称为空。

现在我使用以下代码更改标签名称:

MainMenu frm = new MainMenu();


                try
                {
                    con = new SqlConnection(cs.DBConn);
                    con.Open();
                    String sql = "SELECT SSUserID from SSUserInformation where UserName like '" + txtUserName.Text + "%'";
                    cmd = new SqlCommand(sql, con);
                    rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    while (rdr.Read())
                    {
                        frm.lbluserid.Text = rdr[0].ToString();
                    }
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

然后我使用以下代码将标签名称保存到数据库:

insert into UserInfo(UserID) Values(frm.lbluserid.Text);

问题是recored,但是为空而没有存储到数据库中的任何值

2 个答案:

答案 0 :(得分:-1)

如果你改变你的sql怎么办

String sql = "INSERT INTO UserInfo(UserID) " +
"SELECT SSUserID from SSUserInformation where UserName like '" + txtUserName.Text + "%'";

修改

string SQL = "INSERT INTO [UserInfo] ([UserID]) VALUES (@UserName) " + 
" SELECT [SSUserID] FROM [SSUserInformation] WHERE [UserName] LIKE @UserName + '%' "; 
SqlCommand cmd = new SqlCommand(SQL);
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text); 
cmd.ExecuteNonQuery();

答案 1 :(得分:-1)

 public static string SaveSales(string aa, string bb, string cc, string dd, string ee, string ff, string gg,string hh,string ii)
        {

            OleDbConnection myConnection = GetConnection();
            string myQuery = "INSERT INTO Sales VALUES ( '" + aa + "' , '" + bb + "','" + cc + "','" + dd + "','" + ee + "' ,'" + ff + "' ,'" + gg + "','" + hh + "','" + ii + "'  );";
            OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);