C#代码总出错,求修改、、、求指导、、、在线等、、、
两个BUTTON,第一个BUTTON实现查询出某表的列,并创建出与列数对应的TEXTBOX(这段代码没问题):
public void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter("select COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = '" + DropDownList1.Text + "'", con);
DataTable tb = new DataTable();
da.Fill(tb);
con.Close();
for (int i = 1; i <= tb.Rows.Count; i++)
{
TextBox tx = new TextBox();
tx.ID = "textbox" + i.ToString();
Panel1.Controls.Add(tx);
}
}
第二个BUTTON,想实现,往TEXTBOX中输入数据,点击BUTTON,添加进某表中,这段代码总改不好
:
protected void Button5_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("server=(Local);database=hdb;uid=sa;pwd=sa@123");
con.Open();
string[] tb = new string[]{};
for (int i = 1; i <= Panel1.Controls.Count; i++)
{
TextBox t = (TextBox)Panel1.FindControl("textbox[" + i + "]");
tb[i] = t.Text.ToString();
}
string sql = string.Format("insert into dbo." + DropDownList1.Text + " values('" + tb[i] + "')", string.Join(",", tb.ToString()));
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
}
求大神指导啊
------解决方案--------------------看错了,你改的有问题,sql字串那句
string sql = string.Format("insert into dbo." + DropDownList1.Text + " values('" + values.Substring(0,?values.Length?-?2) + "')", string.Join(",", tb.ToString()));
你的values是sql语句里的子串,还是要再双引号下的,新的values是创建的变量,要在引号外面的++中间