本文介绍了将列表框中的数据插入C#中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个asp.net Web表单,其中包含一些列表框,文本框,下拉列表等.列表框的选择模式是多种.当我运行代码并在各个框中输入值并单击执行"按钮时,将返回包含选定项目的同一页面,并且数据不会进入数据库.
怎么办....请帮助:(
代码是:

Ive made a asp.net webform containing some listboxes,textboxes, dropdown lists etc. The selection mode for list box is multiple. When I run my code and enter the values in the various boxes and click the go button, the same page is returned with selected items and the data doesn''t go into the database.
What to do....Plz help :(
The code is:

private void Postjob(string username,string companyname,string locations,string experiencemin,string experiencemax,string keyskills, string funcArea,string Industry, string postdate, string about, string desc)
    {
        SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Project3\\App_Data\\ojb1.mdf;Integrated Security=True;User Instance=True");
        String sql = "INSERT INTO db(username,Companyname,Locations,Experiencemin,Experiencemax,Keyskills,Functionarea,Industry,Posted_date,About_Company,Job_Description)" +"VALUES(@userName,@Companyname,@Locations,@Experiencemin,@Experiencemax,@Keyskills,@Functionarea,@Industry,@Posted_date,@About_Company,@Job_Description)";

        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlParameter[] param = new SqlParameter[11];

            param[0] = new SqlParameter("@userName", SqlDbType.VarChar, 50);
            param[1] = new SqlParameter("@Companyname", SqlDbType.VarChar, 50);
            param[2] = new SqlParameter("@Locations", SqlDbType.VarChar, 100);
            param[3] = new SqlParameter("@Experiencemin", SqlDbType.VarChar, 50);
            param[4] = new SqlParameter("@Experiencemax", SqlDbType.VarChar, 50);
            param[5] = new SqlParameter("@Keyskills", SqlDbType.VarChar, 1000);
            param[6] = new SqlParameter("@Functionarea", SqlDbType.VarChar, 50);
            param[7] = new SqlParameter("@Industry", SqlDbType.VarChar, 50);
            param[8] = new SqlParameter("@Posted_date", SqlDbType.VarChar, 50);
            param[9] = new SqlParameter("@About_Company", SqlDbType.VarChar, 1000);
            param[10] = new SqlParameter("@Job_Description", SqlDbType.VarChar, 1000);


            param[0].Value = username;
            param[1].Value = companyname;
            param[2].Value = locations;
            param[3].Value = experiencemin;
            param[4].Value = experiencemax;
            param[5].Value = keyskills;
            param[6].Value = funcArea;
            param[7].Value = Industry;
            param[8].Value = postdate;
            param[9].Value = about;
            param[10].Value = desc;

               for (int i = 0; i < param.Length; i++)
                {
                    cmd.Parameters.Add(param[i]);
                }
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            
            

        }
        catch (SqlException ex)
        {
            string msg = "Insert Error";
            msg += ex.Message;
            throw new Exception(msg);

        }
        finally
        {
            con.Close();
        }
    }





protected void Submit_Click(object sender, EventArgs e)
   {
       if (CompanyName.Text != "" && ListBoxLoc.SelectedItem.Text != "" && Expmin.Text != "" && Expmax.Text != "" && Keyskill.Text != "" && ListBoxFun.SelectedItem.Text != "" && ListBoxInd.SelectedItem.Text != "" && abtcomp.Text != "" && JobDesc.Text != "")
       {
           Postjob(username.Text, CompanyName.Text, ListBoxLoc.SelectedItem.Text, Expmin.Text, Expmax.Text, Keyskill.Text, ListBoxFun.SelectedItem.Text, ListBoxInd.SelectedItem.Text, postdate.Text, abtcomp.Text, JobDesc.Text);
           Response.Redirect("Home.aspx");
           
       }



推荐答案



Quote:

SqlConnection connectionSql =新的SqlConnection(数据源=(本地);集成安全性= sspi");

SqlCommand命令Sql =新的SqlCommand();

commandSql.Connection = connectionSql;

commandSql.CommandText ="INSERT INTO dbo.Employee(EmployeeName)VALUES(@employeeName)";

commandSql.Parameters.Add("@ employeeName");

试试

{

connectionSql.Open();

for(int indexCounter = 0; indexCounter< listBoxEmployeeName.Items.Count; indexCounter ++)

{

commandSql.Parameters ["employeeName"].Value = listBoxEmployeeName.Items [indexCounter] .Text;

commandSql.ExecuteNonQuery();

}


connectionSql.Close();

}

捕获(ExceptionExceptionMessage)

{

MessageBox.Show(exceptionMessage.ToString());

}

终于

{
commandSql.Dispose()

connectionSql.Close();

connectionSql.Dispose();

}

MessageBox.Show(保存到数据库!");

SqlConnection connectionSql = new SqlConnection("Data Source=(local);Integrated Security=sspi");

SqlCommand commandSql = new SqlCommand();

commandSql.Connection=connectionSql;

commandSql.CommandText="INSERT INTO dbo.Employee (EmployeeName) VALUES (@employeeName)";

commandSql.Parameters.Add("@employeeName");

try

{

connectionSql.Open();

for (int indexCounter = 0; indexCounter < listBoxEmployeeName.Items.Count; indexCounter++)

{

commandSql.Parameters["employeeName"].Value=listBoxEmployeeName.Items[indexCounter].Text;

commandSql.ExecuteNonQuery();

}


connectionSql.Close();

}

catch (Exception exceptionMessage)

{

MessageBox.Show(exceptionMessage.ToString());

}

finally

{
commandSql.Dispose()

connectionSql.Close();

connectionSql.Dispose();

}

MessageBox.Show("Saved to Database!");


这篇关于将列表框中的数据插入C#中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 18:10