SQL联系人应用程序无法正确响应服务器

时间:2019-02-22 11:34:22

标签: c# sql-server

我刚完成一个联系人应用程序,基本上您在其中输入了联系人详细信息,它将保存到SQL数据库中。 如果我的代码使所有人感到困惑,我感到非常抱歉,因为我是编码方面的大手笔。

连接字符串

<add name="connstrng" connectionString="Data Source=DESKTOP-MJ61J7L\SQLEXPRESS;Initial Catalog=Econtact;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>

C#

class contactClass
{
    //getter and setter properties
    //acts as data carrier in our application

    public int ContactID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string ContactNo { get; set; }

    public string Address { get; set; }

    public string Gender { get; set; }

    static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

    //selecting data from database
    public DataTable Select() {
        //Database Connection
        SqlConnection conn = new SqlConnection(myconnstrng);
        DataTable dt = new DataTable();

        try
        {
            //Writing sql query
            string sql = "SELECT * FROM tbl_contact";
            //creating cmd using sql and conn
            SqlCommand cmd = new SqlCommand(sql, conn);
            //creating sql dataAdapter using cmd
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            conn.Open();
            adapter.Fill(dt);

        } catch(Exception e)
        {


        } finally
        {

            conn.Close();

        }
        return dt;
    }
    //inserting data into DataBase
    public bool Insert (contactClass c)
    {
        //creating a default return type and setting its value to false
        bool isSuccess = false;
        //Connect to DataBase
        SqlConnection conn = new SqlConnection(myconnstrng);

        try
        {
            string sql = "INSERT INTO tbl_contact (FirstName, LastName, ContactNo, Address, Gender) VALUES (@FirstName, @LastName, @ContactNo, @Address, @Gender) ";
            //creating cmd using sql and conn
            SqlCommand cmd = new SqlCommand(sql, conn);
            //Inserting Parameters into tbl_contact
            cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
            cmd.Parameters.AddWithValue("@LastName", c.LastName);
            cmd.Parameters.AddWithValue("@ContactNo", c.ContactNo);
            cmd.Parameters.AddWithValue("@Addresss", c.Address);
            cmd.Parameters.AddWithValue("@Gender", c.Gender);

            conn.Open();
            int row = cmd.ExecuteNonQuery();
            //if the query runs successfully then the value of the rows will be != 0 (because the default it 0) 

            if(row > 0)
            {
                isSuccess = true;
            } else
            {
                isSuccess = false;
            }

        } catch(Exception e)
        {



        } finally
        {
            conn.Close();
        }



        return isSuccess;



    }

    //method to update data in our database from our application
    public bool Update(contactClass c)
    {
        bool isSuccess = false;
        SqlConnection conn = new SqlConnection(myconnstrng);
        try
        {
            string sql = "UPDATE tbl_contact SET FirstName=@FirstName, LastName=@LastName, ContactNo=@ContactNo, Address=@Address, Gender=@Gender WHERE ContactID=@ContactID";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@FirstName", c.FirstName);
            cmd.Parameters.AddWithValue("@LastName", c.LastName);
            cmd.Parameters.AddWithValue("@ContactNo", c.ContactNo);
            cmd.Parameters.AddWithValue("@Addresss", c.Address);
            cmd.Parameters.AddWithValue("@Gender", c.Gender);
            //open database connection
            conn.Open();

            int row = cmd.ExecuteNonQuery();

            if (row > 0)
            {
                isSuccess = true;

            }
            else
            {
                isSuccess = false;

            }



        }
        catch (Exception e)
        {





        }
        finally
        {
            conn.Close();



        }



        return isSuccess;

    }

    //method to delete data from our database
    public static bool Delete(contactClass c)
    {
        bool isSuccess = false;
        SqlConnection conn = new SqlConnection(myconnstrng);

        try
        {
            //sql to delete data
            string sql = "DELETE FROM tbl_contact WHERE ContactID=@ContactID";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@ContactID", c.ContactID);
            //open sql connection
            conn.Open();
            int rows = cmd.ExecuteNonQuery();
            //runs the isSuccess variable if statement

            if (rows > 0)
            {
                isSuccess = true;

            }
            else
            {
                isSuccess = false;

            }
        }
        catch(Exception e)
        {

        }
        finally
        {
            //Close sql connection
            conn.Close();
        }

        return isSuccess;
        }
  }

添加按钮

private void btnAdd_Click(object sender, EventArgs e)
        {
            //Get the value from the input fields
            c.FirstName = txtboxFirstName.Text;
            c.LastName = txtboxLastName.Text;
            c.ContactNo = txtboxPhonenumber.Text;
            c.Address = txtboxAddress.Text;
            c.Gender = cmbGender.Text;
            c.FirstName = txtboxFirstName.Text;

            //inserting data into the database using the method we created in the last video
            bool success = c.Insert(c);

            if (success == true)
            {
                MessageBox.Show("New contact successfully created.");

            }
            else
            {
                MessageBox.Show("Failed to add contact. Try Agian.");
            }
        }

我确实认为这可能是由于与数据库的连接所致。...

我的输出中也收到了此消息-

Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll

Idk,尽管这意味着任何意义。

1 个答案:

答案 0 :(得分:0)

@mjwills地址拼写错误(两次)。那会解释你所看到的。

谢谢,伙计,很明显。我真的很感激,我永远不会发现它。