如何将联系表单连接到Microsoft SQL Server

时间:2014-02-03 05:10:38

标签: c# asp.net sql-server data-binding ado.net

我正在尝试使用c#将我的联系表单连接到数据库,以便所有输入都存储在数据库中,但我很难做到这一点。我还没有我的数据库代码,因为我收到了很多错误。

以下是我的联系表格的代码:

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

namespace CONTACT_US
{
    public partial class Contact_Form : System.Web.UI.Page
    {
        protected void Button2_Click(object sender, EventArgs e)
        {
            System.IO.StreamWriter objWriter;
            objWriter = new System.IO.StreamWriter("C:\\Contact_us.txt",true);

            objWriter.Write("Name: " + TextBox1.Text + ", " + TextBox8.Text + " " + TextBox9.Text + Environment.NewLine);
            TextBox1.Text = string.Empty`enter code here`;
            TextBox8.Text = string.Empty;
            TextBox9.Text = string.Empty;

            objWriter.Write("Email Address: " + TextBox2.Text + Environment.NewLine);
            TextBox2.Text = string.Empty;

            objWriter.Write("Company: " + TextBox3.Text + Environment.NewLine);
            TextBox3.Text = string.Empty;

            objWriter.Write("Contact Number: " + TextBox11.Text + Environment.NewLine);
            TextBox11.Text = string.Empty;

            objWriter.Write("Subject: " + DropDownList2.Text + Environment.NewLine);
            DropDownList2.Text = string.Empty;

            objWriter.Write("Message: " + TextBox6.Text + Environment.NewLine);
            TextBox6.Text = string.Empty;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Confirm", "alert('Thank you for contacting us. We will be in touch shortly.')", true);     
            }
         }
     }

如何将此连接到数据库?

1 个答案:

答案 0 :(得分:0)

基本上在SQL Server或SQL Server Express中创建一个数据库(免费),并添加一个名为联系人的表。将以下列添加到表中。[我假设您具有创建sql表的基本知识等] ID - 这是主键,具有自动增量的整数值 名称 - nvarchar Email_Address - nvarchar 公司 - nvarchar Contact_Number - nvarchar 主题 -nvarchar 消息 - nvarchar

然后在 Contact_Form 类中,使用类似代码/此代码。

public void InsertMethod(){

    SqlConnection sqlConn = null;
    SqlCommand sqlCmd = null;

    try
    {
        String sqlConnectionString = "your database connection string";
        //create an sql connection, providing sql connection string
        sqlConn = new SqlConnection(sqlConnectionString);
        //open the connection
        sqlConn.Open();  
        //insert statement with column names and parameters(@Name, @Email etc, the way how we can pass arguments for insert statement)
        String sqlCommandString =   "INSERT INTO Contact (Name, Email_Address, Company, Contact_Number, Subject, Message) " +
                                    "VALUES(@Name, @Email_Address, @Company, @Contact_Number, @Subject, @Message);";
        // Create the command object using sql connection string & sql connection
        sqlCmd = new SqlCommand(sqlCommandString, sqlConn);
        //add values to the provided parameters
        sqlCmd.Parameters.AddWithValue("@Name", TextBox1.Text);
        sqlCmd.Parameters.AddWithValue("@Email_Address", TextBox2.Text);
        sqlCmd.Parameters.AddWithValue("@Company", TextBox3.Text);
        sqlCmd.Parameters.AddWithValue("@Contact_Number", TextBox4.Text);
        sqlCmd.Parameters.AddWithValue("@Subject", TextBox5.Text);
        sqlCmd.Parameters.AddWithValue("@Message", TextBox6.Text);
        //execute the SQL command(rowsAdded will give the number of rows added to the database)
        int rowsAdded = sqlCmd.ExecuteNonQuery();

    }catch (Exception ex)
    {
        throw (ex);
    }
    finally
    {
        //closing the sql conncetion
        sqlConn.Close();
        //disposing
        sqlCmd.Dispose();
        sqlConn.Dispose();
    }}`

上述方法是使用ADO.Net插入数据的最基本方法。除此之外,你可以使用像LinqToSql,EF等技术。希望这会有所帮助。