在asp.net网站上访问SQL Server [本地计算机]时遇到问题? [英] Problem accessing SQL Server [Local computer] in asp.net website ?

查看:51
本文介绍了在asp.net网站上访问SQL Server [本地计算机]时遇到问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我正在设计一个注册系统,在该系统中,我希望信息(名称,用户名,通行证,国家/地区)应转到SQL Server Express中的表.

数据库信息:我创建了一个ShareSignup数据库,其中有一个名为tblReg的表,其中包含名称,用户名,通行证,国家/地区列.

该数据库位于SQL Server 2008 Express的本地计算机(我的计算机)中,我使用MyPC \ SQLEXPRESS作为服务器名称登录到SQL Server.

现在我正在设计一个注册系统,其中有一个SignUp.aspx页面,我在web.config页面中添加了以下代码(用于连接数据库)

Hi all

I was designing an registration system in which i want that the info (Name, user name, pass, country) should go to the table in SQL server express.

Database Info : i have created an ShareSignup database in which there is one table named tblReg having columns Name, Username, pass, country.

The database is in local computer (my computer) in SQL Server 2008 Express and i use MyPC\SQLEXPRESS as server name to login to the SQL Server.

Now i was designing a registration system in which i have a SignUp.aspx page, i added following code to web.config page (for connecting to Database)

<connectionstrings>

    <add name="MyCon" connectionstring="server=(local);Initial Catalog=ShareSignup;Integrated Security=SSPI;" providername="System.Data.SqlClient" />

  </connectionstrings>



现在,在下一个SignUp.aspx.cs页面上,我添加了代码



Now on the next for SignUp.aspx.cs page i added the code

public string GetConnectionString()
        {

            //sets the connection string from your web config file "ConnString" is the name of your Connection String

           return System.Configuration.ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;

          

        }


        private void ExecuteInsert(string name, string uname, string pass, string country)
        {
            SqlConnection conn = new SqlConnection(GetConnectionString());

            string sql = "INSERT INTO tblReg (Name, UserName, Password,Country) VALUES "

                          + " (@Name,@UserName,@Password,@Country)";

            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] param = new SqlParameter[4];

                param[0] = new SqlParameter("@Name", SqlDbType.VarChar, 50);

                param[1] = new SqlParameter("@UserName", SqlDbType.VarChar, 50);

                param[2] = new SqlParameter("@Password", SqlDbType.VarChar, 50);

                param[3] = new SqlParameter("@Country", SqlDbType.VarChar, 15);

                param[0].Value = name;
                param[1].Value = uname;
                param[2].Value = pass;
                param[3].Value = country;

                for (int i = 0; i < param.Length; i++)
                {

                    cmd.Parameters.Add(param[i]);

                }



                cmd.CommandType = CommandType.Text;

                cmd.ExecuteNonQuery();
            }

            catch (System.Data.SqlClient.SqlException ex)
            {

                string msg = "Insert Error:";

                msg += ex.Message;

                throw new Exception(msg);

            }

            finally
            {

                conn.Close();

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TxtPassword.Text == TxtRePassword.Text)
            {

                //call the method to execute insert to the database

                ExecuteInsert(TxtName.Text, TxtUserName.Text, TxtPassword.Text, TxtAddress.Text);



                Response.Write("Record was successfully added!");



              
            }

            else
            {

                Response.Write("Password did not match");

                TxtPassword.Focus();

            }
        }



现在,在运行页面并单击Submit(button1)时,出现此错误.

对象引用未设置为对象的实例.

它在第
行显示错误
System.Configuration.ConfigurationManager.ConnectionStrings ["MyCon"].ConnectionString;

可能是什么问题?

可能在web.config代码中?

或者可以通过设置< connectingString> ?

请帮帮我

谢谢



Now on runing the page and hitting Submit (button1) i get this error.

Object reference not set to an instance of an object.

and it shows the error at line

System.Configuration.ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;

what can be the problem ?

may be in web.config code ?

or may be with setting <connectingString> ?

Please help me out

Thanks

推荐答案

检查配置文件是否包含connectionStrings中MyCon的条目.
Check the configuration file contains an entry for MyCon in connectionStrings.


尝试使用此

Try using this

connectionstring= "Server=myServerAddress;Database=myDataBase;Integrated Security=True;Asynchronous Processing=True;"

or 

connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=myDataBase;Integrated Security=True"


仍然没有遇到同样的问题.

请帮帮我
No still the same problem faced.

Please help me out


这篇关于在asp.net网站上访问SQL Server [本地计算机]时遇到问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆