前面主要好似没表述清楚,问题是这样的:
我想要:根据用户的选择往数据库里面加内容,
如我放了4个checkBox,分别是1-4,用户可以任意的选择0-4个checkBox!然后将用户选中的checkBox的text,插入到数据库的tb(userName)字段中,就这样,下面的一些代码是我写的,写的不对,仅做参考!!
protected void Button1_Click(object sender, EventArgs e)
{
DBMethod MyDB = new DBMethod();
string str1, str2, str3, str4;
string str;
//string[] str = new string[100];
if (CheckBox1.Checked == true)
str1 = CheckBox1.Text;
else
str1 = "";
if (CheckBox2.Checked == true)
str2 = CheckBox2.Text;
else
str2 = "";
if (CheckBox3.Checked == true)
str3 = CheckBox3.Text;
else
str3 = "";
if (CheckBox4.Checked == true)
str4 = CheckBox4.Text;
else
str4 = "";
for (int i = 1; i <= 4; i++)
{
str = str + i.ToString();
//String SqlString = "insert into tb values(" + str[i].ToString() + ")";
String SqlString = "insert into tb(userName) values('"+ str.ToString () + "')";
// string SqlString = "insert into tb values('" + str + "')";
MyDB.ExecuteSQL(SqlString);
}
}
}
我想要:根据用户的选择往数据库里面加内容,
如我放了4个checkBox,分别是1-4,用户可以任意的选择0-4个checkBox!然后将用户选中的checkBox的text,插入到数据库的tb(userName)字段中,就这样,下面的一些代码是我写的,写的不对,仅做参考!!
protected void Button1_Click(object sender, EventArgs e)
{
DBMethod MyDB = new DBMethod();
string str1, str2, str3, str4;
string str;
//string[] str = new string[100];
if (CheckBox1.Checked == true)
str1 = CheckBox1.Text;
else
str1 = "";
if (CheckBox2.Checked == true)
str2 = CheckBox2.Text;
else
str2 = "";
if (CheckBox3.Checked == true)
str3 = CheckBox3.Text;
else
str3 = "";
if (CheckBox4.Checked == true)
str4 = CheckBox4.Text;
else
str4 = "";
for (int i = 1; i <= 4; i++)
{
str = str + i.ToString();
//String SqlString = "insert into tb values(" + str[i].ToString() + ")";
String SqlString = "insert into tb(userName) values('"+ str.ToString () + "')";
// string SqlString = "insert into tb values('" + str + "')";
MyDB.ExecuteSQL(SqlString);
}
}
}
这是什么? str1和str2str3str4的值你不是一个都没取到吗
string [] strs=new string[]{str1,str2,str3,str4};
for (int i = 0; i <= strs.Length; i++)
str = str + i.ToString();
String SqlString = "insert into tb(userName) values('"+ strs[i].ToString () + "')";
MyDB.ExecuteSQL(SqlString);
}
protected void Button1_Click(object sender, EventArgs e)
{
DBMethod MyDB = new DBMethod();
string[] str = new string[5];
if (CheckBox1.Checked == true)
str[1] = CheckBox1.Text;
else
str[1] = "";
if (CheckBox2.Checked == true)
str[2] = CheckBox2.Text;
else
str[2] = "";
if (CheckBox3.Checked == true)
str[3] = CheckBox3.Text;
else
str[3] = "";
if (CheckBox4.Checked == true)
str[4] = CheckBox4.Text;
else
str[4] = "";
for (int i = 1; i <= 4; i++)
{
if(str[i]=="")
continue;
String SqlString = "insert into tb(userName) values('"+ str[i] + "')"; MyDB.ExecuteSQL(SqlString);
}
}
}
{
DBMethod MyDB = new DBMethod(); string str_field = "";
string str_value = ""; if (CheckBox1.Checked == true)
{
str_field = "字段1";
str_value = "'" + CheckBox1.Text + "'";
} if (CheckBox2.Checked == true)
{
str_field = ((str_field == "") ? "" : ",") + "字段2";
str_value = ((str_value == "") ? "" : ",") + "'" + CheckBox2.Text + "'";
} if (CheckBox3.Checked == true)
{
str_field = ((str_field == "") ? "" : ",") + "字段3";
str_value = ((str_value == "") ? "" : ",") + "'" + CheckBox3.Text + "'";
} if (CheckBox4.Checked == true)
{
str_field = ((str_field == "") ? "" : ",") + "字段4";
str_value = ((str_value == "") ? "" : ",") + "'" + CheckBox4.Text + "'";
} String SqlString = "insert into tb(" + str_field + ") values(" + str_value + ")"; MyDB.ExecuteSQL(SqlString); }
应该 protected void Button1_Click(object sender, EventArgs e)
{
DBMethod MyDB = new DBMethod(); string str_field = "";
string str_value = ""; if (CheckBox1.Checked == true)
{
str_field = "字段1";
str_value = "'" + CheckBox1.Text + "'";
} if (CheckBox2.Checked == true)
{
str_field += ((str_field == "") ? "" : ",") + "字段2";
str_value += ((str_value == "") ? "" : ",") + "'" + CheckBox2.Text + "'";
} if (CheckBox3.Checked == true)
{
str_field += ((str_field == "") ? "" : ",") + "字段3";
str_value += ((str_value == "") ? "" : ",") + "'" + CheckBox3.Text + "'";
} if (CheckBox4.Checked == true)
{
str_field += ((str_field == "") ? "" : ",") + "字段4";
str_value += ((str_value == "") ? "" : ",") + "'" + CheckBox4.Text + "'";
} String SqlString = "insert into tb(" + str_field + ") values(" + str_value + ")"; MyDB.ExecuteSQL(SqlString); }没有调试,不过思路这样。
每检查一个checkbox,如果选择了,就将字段名和值加入各自的字符串,没选就算了。最后一并生产SQL。
字段用逗号隔开,值用引号加逗号隔开。