如何将ListBox中内容写入MySQL数据库?请帮忙分析下代码
左侧的LISTBOX中的数据是绑定数据库读出来的
右边的LISTBOX中的数据是左边LISTBOX选择后添加出来的
我现在需要将右边的LISTBOX中的多项内容添加到另外一个表的某一字段中,如何进行?
点击代码如下:
protected void ButtonAdd_Click(object sender, EventArgs e)
{
int count = this.PatrolListInfo.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem item = PatrolListInfo.Items[index];
if (PatrolListInfo.Items[index].Selected == true)
{
TaskListInfo.Items.Add(item);
//index--;
}
index++;
}
}
protected void ButtonRemove_Click(object sender, EventArgs e)
{
int count = this.TaskListInfo.Items.Count;
int index = 0;
for (int i = 0; i < count; i++)
{
ListItem item = TaskListInfo.Items[index];
if (TaskListInfo.Items[index].Selected == true)
{
TaskListInfo.Items.Remove(item);
index--;
}
index++;
}
}
更新数据库
public void TaskInfo()
{
int count = this.TaskListInfo.Items.Count;
for (int i = 0; i < count; i++)
{
string item = TaskListInfo.Items[i].ToString();
string sql = string.Format("UPDATE taskbrowse SET TaskPatrolInfo='{0}' WHERE TaskId='{1}'",item.Trim(),this.TextTaskId.Text.Trim());
MySqlConnection conn = new MySqlConnection("server=192.168.0.254;user id=root;password=110120110;database=dlxj;charset=gb2312");
conn.Open();
MySqlCommand myCommand = new MySqlCommand(sql, conn);
myCommand.ExecuteNonQuery();
conn.Close();
}
}
请大家帮忙看下代码
------解决方案--------------------
把你更新的方法改一下,
public void TaskInfo_Insert()
{
int count = this.TaskListInfo.Items.Count;
for (int i = 0; i < count; i++)
{
string item = TaskListInfo.Items[i].ToString();
string sql = string.Format("insert into taskbrowse(TaskPatrolInfo) values='{0}' ",item.Trim());
MySqlConnection conn = new MySqlConnection("server=192.168.0.254;user id=root;password=110120110;database=dlxj;charset=gb2312");
conn.Open();
MySqlCommand myCommand = new MySqlCommand(sql, conn);
myCommand.ExecuteNonQuery();
conn.Close();
}
}