循环连接字符串的问题!————————————————急!
cs代码先贴上:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class News_News_Sections_Edit : System.Web.UI.Page
{
ConnectionDb db = new ConnectionDb();
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
//public static string max_id;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
additmes();
//max_id = getMaxId();
//Response.Write(max_id);
}
}
//綁定dropdownlsit
public void additmes()
{
string items_id = " ";
string name = " ";
//獲取所有link字段
string allitems = "select LINK from NEWS_SECTIONS order by LINK ";
//獲取數據集
ds1 = db.getDataSet(allitems, "NEWS_SECTIONS ");
//字段斷行數
int num = ds1.Tables[ "NEWS_SECTIONS "].Rows.Count;
//從1取值
for (int i = 0; i < num; i++)
{
string addstr = " ";
//循環賦值
items_id = ds1.Tables[ "NEWS_SECTIONS "].Rows[i][0].ToString();
//Response.Write(items_id + " <br /> ");
string[] arr = items_id.Split( ', ');
for (int j = 0; j < arr.Length; j++)
{
string getname = "select SECTIONS_NAME from NEWS_SECTIONS where SECTIONS_ID= "+arr[j];
ds2 = db.getDataSet(getname, "NEWS_SECTIONS ");
name = ds2.Tables[ "NEWS_SECTIONS "].Rows[0][0].ToString();//獲取名字
addstr += "> " + name;
//Response.Write(addstr+ " <br /> ");
}
//parent.Items.Add(items_id);
parent.Items.Add(addstr);
addstr = " ";
}
}
//添加欄目
protected void save_ServerClick(object sender, EventArgs e)
{
DataSet ds = new DataSet();
//獲取結搆層次
string folder = parent.SelectedValue;
//去掉最開始的 "> "
folder = folder.Substring(1,folder.Length-1);
string[] folder_arr = folder.Split( '> ');
//Response.Write(folder_arr.Length.ToString());
for (int i = 0; i < folder_arr.Length; i++)
{
string sid = " ";
//把名稱轉換成id
string getid = "select SECTIONS_ID from NEWS_SECTIONS where SECTIONS_NAME= ' " + folder_arr[i] + " ' ";
ds = db.getDataSet(getid, "NEWS_SECTIONS ");
sid += ds.Tables[ "NEWS_SECTIONS "].Rows[0][0].ToString()+ ", "+getMaxId();
Response.Write(sid);
}
}
//獲取max_id
public string getMaxId()
{
DataSet ds = new DataSet();
string getmaxid = "select (max(SECTIONS_ID)+1) from NEWS_SECTIONS ";
ds = db.getDataSet(getmaxid, "NEWS_SECTIONS ");
return ds.Tables[ "NEWS_SECTIONS "].Rows[0][0].ToString();
}
}
数据库为:
SECTIONS_IDLINK SECTIONS_NAME
0 0 首頁
1 0,1 通知
2 0,2 公告
3 0,3 通報
4 0,1,4 內部通知
5 0,1,5 外部通知
6 0,2,6 一般公告
7 0,2,7 重要公告
8 0,1,4,8 人事部通知
9 0,1,4,9 技術部通知
10 0,2,6,10 內部公告
在选择了dropdownlist的项后,例如选择的是:> 首页> 通知> 内部通知,然后点“添加”按钮应该调用上面的getMaxId()方法生成sections_id为11的新数据,然后LINK字段要变为“0,1,4,11”,但是结果却是 "0,111,114,11 "我知道是循环出错了,但是找不到,急救!!!
[解决办法]
比较长,漫漫看