将SQL Server链接到Visual Studio

发布于 2025-02-07 18:06:13 字数 1894 浏览 3 评论 0原文

我一直在为我的Web应用程序提供注册页面,但是我面临将数据库连接到网络应用程序的问题。前端编码是正确的,并且没有错误,

这是OnClick方法中的后端代码:

SqlConnection conn = new SqlConnection(@"Data Source=TARISAI;" +
      "Initial Catalog=FarmCentral;" +
      "Integrated Security=SSPI;");
    
      
    
    try
    {

        //SqlConnection conn = new SqlConnection(strcon);
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }

        //insert values from web application to SQL database
        SqlCommand cmd = new SqlCommand("INSERT INTO employee_tbl (full_name, date_of_birth, contact, email, province, city, postcode, address, employeeID, Password) " +
            "values(@full_name, @date_of_birth, @contact, @email,@province, @city, @postcode, @address, @employeeID, @Password)", conn);


        // link data to textboxes
        cmd.Parameters.AddWithValue("@full_name", namebox.Text.Trim());
        cmd.Parameters.AddWithValue("@date_of_birth", dobbox.Text.Trim());
        cmd.Parameters.AddWithValue("@contact", contactbox.Text.Trim());
        cmd.Parameters.AddWithValue("@email", emailbox.Text.Trim());
        cmd.Parameters.AddWithValue("@province", provincedrop.SelectedItem.Value);
        cmd.Parameters.AddWithValue("@city", citybox.Text.Trim());
        cmd.Parameters.AddWithValue("@post code", postcodebox.Text.Trim());
        cmd.Parameters.AddWithValue("@employeeID", employeeIDbox.Text.Trim());
        cmd.Parameters.AddWithValue("@Password", passbox.Text.Trim());

        cmd.ExecuteNonQuery();
        conn.Close();
        Response.Write("<script>alert('Sign Up Successful. Go to Employee Login to Login');</script>");
    }
    catch(Exception exx)
    {

        Response.Write("<script>alert('" + exx.Message + "');</script>");

    } 

当我单击时,INGIS INMING IN imp Ress会刷新页面,但不执行操作。没有提示,并且值未添加到数据库中。我错过了什么还是我做错了

I have been working on a sign Up page for my web app, however I am facing issues with connecting my database to my web app. The frontend coding is proper and has no errors

This is the backend code inside the Onclick method:

SqlConnection conn = new SqlConnection(@"Data Source=TARISAI;" +
      "Initial Catalog=FarmCentral;" +
      "Integrated Security=SSPI;");
    
      
    
    try
    {

        //SqlConnection conn = new SqlConnection(strcon);
        if (conn.State == ConnectionState.Closed)
        {
            conn.Open();
        }

        //insert values from web application to SQL database
        SqlCommand cmd = new SqlCommand("INSERT INTO employee_tbl (full_name, date_of_birth, contact, email, province, city, postcode, address, employeeID, Password) " +
            "values(@full_name, @date_of_birth, @contact, @email,@province, @city, @postcode, @address, @employeeID, @Password)", conn);


        // link data to textboxes
        cmd.Parameters.AddWithValue("@full_name", namebox.Text.Trim());
        cmd.Parameters.AddWithValue("@date_of_birth", dobbox.Text.Trim());
        cmd.Parameters.AddWithValue("@contact", contactbox.Text.Trim());
        cmd.Parameters.AddWithValue("@email", emailbox.Text.Trim());
        cmd.Parameters.AddWithValue("@province", provincedrop.SelectedItem.Value);
        cmd.Parameters.AddWithValue("@city", citybox.Text.Trim());
        cmd.Parameters.AddWithValue("@post code", postcodebox.Text.Trim());
        cmd.Parameters.AddWithValue("@employeeID", employeeIDbox.Text.Trim());
        cmd.Parameters.AddWithValue("@Password", passbox.Text.Trim());

        cmd.ExecuteNonQuery();
        conn.Close();
        Response.Write("<script>alert('Sign Up Successful. Go to Employee Login to Login');</script>");
    }
    catch(Exception exx)
    {

        Response.Write("<script>alert('" + exx.Message + "');</script>");

    } 

When I click sign up it refreshes the page but does not do the action. There is no prompt and values are not added to database. Am I missing something or have I done it wrong

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2025-02-14 18:06:13

好的,您是否正在运行SQL Server Express?

您是否安装了SQL Studio?

确实,如果没有,请让上述工作。它会帮助您哦。

下一步:

构建连接字符串?

让系统为您做。

因此,您不会在该连接中输入,但是Visual Studio Builder可以为您“创建”和“创建”连接。一个很好的触摸是,它还可以单击测试连接。

更好?

您的连接字符串现在不必在代码中。再次在这里保存世界和您的键盘。

因此,在您的Web项目中,转到这里:

”“在此处输入图像描述”

在上面,这确实很容易,因为您只需按照提示即可。因此,单击[...],然后您可以连接到SQL Server。

因此,我们单击上面,然后得到:

因此,现在所有这些都下落了 - 在这里举行了很多手。

我可以单击服务器 - 出现下拉选择。

数据库也是如此:

例如:

例如:

所以,现在完成后,我有:

“在此处输入图像描述”

好吧,现在在代码中吗?

这样说:

    //insert values from web application to SQL database
    string strSQL
        = "INSERT INTO employee_tbl (full_name, date_of_birth, contact, email, province, city, postcode, " +
        "address, employeeID, Password) " +
        "values (@full_name, @date_of_birth, @contact, @email,@province, @city, @postcode, @address, @employeeID, @Password)";

    using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
    {
        using (SqlCommand cmd = new SqlCommand(strSQL, conn))
        {
            cmd.Parameters.Add("@full_name", SqlDbType.NVarChar).Value = namebox.Text;
            cmd.Parameters.Add("@date_of_birth", SqlDbType.Date).Value dobbox.Text;
            cmd.Parameters.Add("@contact", SqlDbType.NVarChar).Value = contactbox.Text;
            cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = emailbox.Text;
            cmd.Parameters.Add("@province", SqlDbType.NVarChar).Value = provincedrop.SelectedItem.Value;
            cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = citybox.Text;
            cmd.Parameters.Add("@post code", SqlDbType.NVarChar).Value = postcodebox.Text;
            cmd.Parameters.Add("@employeeID", SqlDbType.Int).Value = employeeIDbox.Text;
            cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = passbox.Text;

            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }

因此,请注意我如何使用我们设置的连接字符串(因此需要应用程序中的一个位置 - 允许您轻松更改它)。

而且您永远不必测试连接是否打开 - 如果您这样做,那么您的代码就会混乱,并且没有遵循上述模式。

Ok, do you have sql server express running?

Do you have sql studio installed?

Really, if not, then get the above working. it will help you oh so much.

next up:

Building the connecting string?

Let the system do that for you.

thus, you don't type in that connection, but BETTER is the Visual Studio builders can "make" and "create" the connection for you. And a nice touch is it ALSO lets you click on test connection.

Even better?

Your connection string now does NOT have to be in code. Again saving the world and your keyboard here.

So, in your web project, go here:

enter image description here

With above, it REALLY easy, since you just follow the prompts. So, click on the [...], and then you can connect to the sql server.

So, we click on above, and get this:

So, now it all drop downs - lots of hand holding here.

I get to click on servers - a drop down selection appears.

Same goes for databases:

eg this:

enter image description here

And after I select the database, I can click on test conneciton.

eg this:

enter image description here

So, now when done, i have this:

enter image description here

Ok, so now in code?

Say this:

    //insert values from web application to SQL database
    string strSQL
        = "INSERT INTO employee_tbl (full_name, date_of_birth, contact, email, province, city, postcode, " +
        "address, employeeID, Password) " +
        "values (@full_name, @date_of_birth, @contact, @email,@province, @city, @postcode, @address, @employeeID, @Password)";

    using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
    {
        using (SqlCommand cmd = new SqlCommand(strSQL, conn))
        {
            cmd.Parameters.Add("@full_name", SqlDbType.NVarChar).Value = namebox.Text;
            cmd.Parameters.Add("@date_of_birth", SqlDbType.Date).Value dobbox.Text;
            cmd.Parameters.Add("@contact", SqlDbType.NVarChar).Value = contactbox.Text;
            cmd.Parameters.Add("@email", SqlDbType.NVarChar).Value = emailbox.Text;
            cmd.Parameters.Add("@province", SqlDbType.NVarChar).Value = provincedrop.SelectedItem.Value;
            cmd.Parameters.Add("@city", SqlDbType.NVarChar).Value = citybox.Text;
            cmd.Parameters.Add("@post code", SqlDbType.NVarChar).Value = postcodebox.Text;
            cmd.Parameters.Add("@employeeID", SqlDbType.Int).Value = employeeIDbox.Text;
            cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = passbox.Text;

            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }

so, note how I used that connection string we setup (thus only ONE place in the applcation is required - allows you to change it with ease).

And you NEVER have to test if the connection is open - if you do, then your code is messed up, and did not follow the above pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文