locked
Connecting to a SQL database on my website. RRS feed

  • Question

  • Hello,

    I'm trying to create a login form that connects to a SQL database that's placed on my website. My problem is that I don't know how to connect to the database. Let's take these for an example:

    Server the database is on: 10.246.16.159:3306

    Database name: rhythmcheaters_

    Database section: phpbb_users

    Database password : 1A2b3C4d




    • Moved by Kristin Xie Thursday, January 15, 2015 9:02 AM
    Tuesday, January 6, 2015 8:34 PM

Answers

All replies

  • Look at www.connectionstring.com for the proper connection string.  I usually use the following :

    Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;

    For the DataSource use the instance of the database that is shown in the login window of SQL Server Management Studio (SSMS).  In most cases it is the computer name (or IP) and a folder name like SQLEXPRESS.


    jdweng

    • Proposed as answer by Kristin Xie Thursday, January 15, 2015 9:01 AM
    • Marked as answer by Just Karl Friday, April 17, 2015 8:05 PM
    Tuesday, January 6, 2015 8:56 PM
  • No ASP.NET or  any Web page for that matter should be making direct contact with a database. You need to understand Seperation of concerns. You need to get that stright before you run off and do things that will get a site hacked.

    http://en.wikipedia.org/wiki/Separation_of_concerns

    http://en.wikipedia.org/wiki/Multitier_architecture

    It's using datasets, datatable and table adapters, but it's a start.:)

    http://msdn.microsoft.com/en-us/library/bb384570.aspx

    If not using MVC, then consider using MVP.

    http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter

    http://polymorphicpodcast.com/shows/mv-patterns

    You should post to the ASP.NET forum.

    http://forums.asp.net/

    • Proposed as answer by Kristin Xie Thursday, January 15, 2015 9:01 AM
    • Marked as answer by Just Karl Friday, April 17, 2015 8:05 PM
    Tuesday, January 6, 2015 8:57 PM
  • Thank you for the reply darnold924 and Joel Engineer.


    The application I'm creating is a private application, that's why I would prefer to do it the less safe way.

    Are there any shorter ways to do this? For example with the 'new SqlConnection();' function?


    • Edited by Green2222 Tuesday, January 6, 2015 9:47 PM
    Tuesday, January 6, 2015 9:42 PM
  • Are there any shorter ways to do this? For example with the 'new SqlConnection();' function?

    The only other way I would do it would be to use Linq-2-SQL or the ADO.NET EntityFramework, which both of them use Linq where I would make a direct connection to the database from a Web page.

    http://www.devx.com/dotnet/Article/34653

    Tuesday, January 6, 2015 9:51 PM
  • Please refer to my post here:

    So, How Do I Get The Connection String?

     

    Noam B.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Proposed as answer by Noam B Thursday, January 8, 2015 11:08 AM
    • Marked as answer by Just Karl Friday, April 17, 2015 8:05 PM
    Wednesday, January 7, 2015 11:40 AM
  • Following Example will May  help You..

    Try to design userinterface as like this..

    Double on Login button a new method will be generated write the following code under that method..

                                       

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Security.Cryptography;

    namespace windowsloginapp
    {

        public partial class login : Form
        {
            public string userName, Password;
            public int usercount;
            public login()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                userName = textBox1.Text;
                Password = textBox2.Text;
                if (userName == "" && Password == "")
                {
                    MessageBox.Show("Please Enter User Name and Password");
                }
                else
                {
                    string scn = ConfigurationManager.ConnectionStrings["Myconn"].ConnectionString.ToString();
                    SqlConnection cn = new SqlConnection(scn);
                     cn.Open();
                     SqlCommand cmd = new SqlCommand("SP_logapp", cn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Username", userName);
                        cmd.Parameters.AddWithValue("@Password", Password);

                        int usercount =(Int32) cmd.ExecuteScalar();
                            if (usercount == 1)
                            {
                                this.Hide();
                                Welcome w1 = new Welcome(userName);
                                w1.Show();
                            }
                            else
                            {
                                //login lg = new login();
                                //lg.Show();
                                MessageBox.Show("Not a Valid UserName and Password");
                            }

                            cn.Close();

                }



            }

        }
    }

    APP.Config 

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>

      <connectionStrings>
        <add name="Myconn" connectionString="Data Source=10.246.16.159:3306;Initial Catalog=rhythmcheaters_; user ID=phpbb_users;Password=1A2b3C4d;Integrated security=sspi" providerName="system.data.sqlclient" />
      </connectionStrings>
    </configuration>

    Create database with the same name of(rhythmcheaters_) and declare 2 variables as username and password....

    Insert some into that table...Do the same web application also..........It will work...


    Thanks & Regards RAJENDRAN M

    Wednesday, January 7, 2015 12:42 PM