[英]Insert statement on asp.net not inserting on mysql table
我正在asp.net上创建一个Web表单,该表单将允许最终用户将在选定部门工作的多个用户分配到一个测验中。
该数据库是mysql数据库,因为我使用的是joomla
mysql上的表为:jos_users_quizzes,其以下列:
id
quiz_id
user_id
我有一秒钟叫做
具有此列的jos_dhruprofile
id
name
username
department
我需要从选定部门中选择所有用户ID,然后将这些ID插入到user_quizzes表中。
我有两个查询试图插入第一个查询,该查询具有所选部门的条件不起作用,而没有插入are语句的查询实际上没有插入,我没有错误,只是插入没有进行。
string quizidselected = DropDownList1.SelectedValue;
string deptselected = ListBox2.SelectedValue;
OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (user_id, quiz_id) SELECT uid, ' " + quizidselected + " ' FROM jos_dhruprofile WHERE department = ' " + deptselected.ToString() + " '");
// OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (user_id, quiz_id) SELECT uid, ' " + quizidselected + " ' FROM dhruprofile ");
非常感谢您查找我的代码
完整代码
代码从和ASP.NET表单插入....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Text;
using System.Data;
using System.Data.Odbc;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
private void InsertRecords(StringCollection sc)
{
string ConnectionString = @"driver={MySQL ODBC 5.1 Driver};server=appdevelsvr;database=xxxx;uid=xx;pwd=xx;";
OdbcConnection conn = new OdbcConnection(ConnectionString);
try
{
conn.Open();
string quizidselected = DropDownList1.SelectedValue;
string deptselected = ListBox2.SelectedValue;
OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (user_id, quiz_id) SELECT uid, ' " + quizidselected + " ' FROM jos_dhruprofile WHERE department = ' " + deptselected.ToString() + " '");
// OdbcCommand cmd = new OdbcCommand("INSERT INTO jos_jquarks_users_quizzes (user_id, quiz_id) SELECT uid, ' " + quizidselected + " ' FROM dhruprofile ");
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);
Response.Write(deptselected.ToString());
// Response.Write(sql.ToString());
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
foreach (ListItem item in ListBox2.Items)
{
if (item.Selected)
{
sc.Add(item.Text);
}
}
InsertRecords(sc);
}
}
我假设您要我们调试的查询是带有where子句的查询。
请在为cmd创建新odbc命令的行上设置断点。 您需要检查“ deptselected”的值。
或者,您可以debug.writeline SQL语句并将其复制粘贴到SQL查询UI中。 请自行运行选择。 我认为它不会返回任何行,因为表中不存在'deptselected'或'deptselected'为空。
另外,还有两个很好的编程指针:
1)。 使用参数化的SQL语句,而不是将值附加到字符串中。 您的代码容易受到SQL注入攻击。 这真是太糟了。
2)。 'deptselected'已经是一个字符串,您无需再次'ToString'它。
希望这可以帮助。
编辑:-我刚刚读了上面的评论。 在该表上执行select *(星号),其中someother_column = another_value以获得与您感兴趣的同一行。检查部门列的字符串长度。 检查字符串开头或之后是否有空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.