我想使用c#将数据插入SQL服务器

时间:2016-10-18 21:01:33

标签: c# sql-server

我使用以下代码将数据插入SQL服务器但未插入数据,当我单击提交按钮时,页面被刷新并且所选文件被添加到相应的文件夹中。

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class company_add : System.Web.UI.Page
{
    string strConnString = ConfigurationManager.ConnectionStrings["JobInnConnectionString1"].ConnectionString;
    string s1;
    string s2;
    string path1;
    string path2;
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SubmitCompany_Click(object sender, EventArgs e)
    {
        if (companylogo.PostedFile.ContentLength > 0)
        {
            s1 = Path.GetFileName(companylogo.FileName);
            path1 = Server.MapPath("Logo") + "/" + s1;
            companylogo.SaveAs(path1);
        }
        if (cover_img_file.PostedFile.ContentLength > 0)
        {
            s2 = Path.GetFileName(cover_img_file.FileName);
            path2 = Server.MapPath("cover_images") + "/" + s2;
            cover_img_file.SaveAs(path2);
        }
        SqlConnection con = new SqlConnection(strConnString);
        con.Open();
        SqlCommand com = new SqlCommand("insertinto_company", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Connection = con;

        com.Parameters.AddWithValue("@Logo", s1);
        com.Parameters.AddWithValue("@Company_Name", companyname.Text.Trim());
        com.Parameters.AddWithValue("@Headline", headline.Text.Trim());
        com.Parameters.AddWithValue("@Short_Description", shortdescription.Text.Trim());
        com.Parameters.AddWithValue("@Location", location.Text.Trim());
        com.Parameters.AddWithValue("@Employer", DropDownListemployer.SelectedValue.Trim());
        com.Parameters.AddWithValue("@Website_Address", webadres.Text.Trim());
        com.Parameters.AddWithValue("@Founded_on", foundedon.Text.Trim());
        com.Parameters.AddWithValue("@Phone_no", phoneno.Text.Trim());
        com.Parameters.AddWithValue("@Email", TextEmail.Text.Trim());
        com.Parameters.AddWithValue("@cover_image", s2);
        com.Parameters.AddWithValue("@facebook_Url", facebuk_url.Text.Trim());
        com.Parameters.AddWithValue("@Twitter_Url", twitter_url.Text.Trim());
        com.Parameters.AddWithValue("@google_url", google_url.Text.Trim());
        com.Parameters.AddWithValue("@youtube_url", youtube_url.Text.Trim());
        com.Parameters.AddWithValue("@company_detail", company_detail.Text.Trim());
    }
}

1 个答案:

答案 0 :(得分:11)

你错过了

com.ExecuteNonQuery();