学生信息表格提供错误 [英] student information form givning an error

查看:62
本文介绍了学生信息表格提供错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发了程序学生信息..

但是在特定代码中出现了一些错误,当我点击那个时候的newstudentbutton错误正在兴起,请解决问题并给我解决方案...

代码..

1.App配置

Hi,
I am made the program student information ..
but the some error arise in particular code ,When I am Click the newstudentbutton in that time the error is rise, please solve the problem and give me solution...
code..
1.App Configuration

<configuration>
  <configsections>
    <connectionstrings>
      <add name="ConString" connectionstring="Data Source=ANAND;Initial Catalog=TEST;Integrated Security=True"

           providerName ="System.Data.SqlClient"/>
     </connectionstrings>
  </configsections>
</configuration>



2.DB_ACCESS类


2.DB_ACCESS Class

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Studentinformation
{
    class DB_Access
    {
        SqlConnection conn;
        public DB_Access()
        {
            conn = DB_Connection.GetConnection();  // in this line the runtime error is rise
         }
        public void add_student(string regNo, string fName, string Lname, string phoneNo)
        {
            if (conn.State.ToString() == "Closed")
            {
                conn.Open();
            }
            SqlCommand newCmd = conn.CreateCommand();
            newCmd.Connection = conn;
            newCmd.CommandType = CommandType.Text;
            newCmd.CommandText = "insert into student values('" + regNo + "','" + fName + "','" + Lname + "','" + phoneNo + "',)";
            newCmd.ExecuteNonQuery();
        }
    }
}



3.


3.

class DB_Connection
   {
       public static SqlConnection NewCon;
       public static string ConStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
       public static SqlConnection GetConnection()
       {
           NewCon = new SqlConnection(ConStr);
           return NewCon;
       }
   }



4.


4.

public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       private void btnAddNew_Click(object sender, EventArgs e)
       {
           frmNewStudent newstudent = new frmNewStudent();
           newstudent.Show();
       }
   }

推荐答案

你需要查看你的DB_Connection类和GetConnection特别是方法。可能是你的连接字符串不正确,Web服务器PC找不到SQL服务器,或者服务器上找不到数据库。



检查:尝试在VS中使用Server Explorer窗格设置连接。

1)打开服务器资源管理器。

2)右键单击数据连接并选择添加连接

3)在随后的对话框中,选择您的数据源和数据库,指定安全信息,然后按测试连接按钮。

4)当连接工作时,按确定

5)在服务器资源管理器窗格中突出显示数据库,然后查看属性窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到您的应用程序或配置文件中。



然后,如果您不发布网站在不同的环境中(例如Web服务器而不是本地PC - SQL服务器实例可能会有所不同)你应该没问题。
You need to look at your DB_Connection class, and the GetConnection method in particular. The chances are that your connection string is incorrect and the web server PC can't find the SQL server, or the database doesn't exist on the server if it can find it.

Check it: Try setting up a connection in VS with the Server Explorer pane.
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

Then, provided you don't publish the website to a different environment (such as a web server instead of your local PC - where the SQL server instance will probably be different) you should be fine.


转到http:// csharp- sqlconnection.blogspot.com/用于数据库连接的简便方法
go to http://csharp-sqlconnection.blogspot.com/ for easy and shortest way for database connectivity


这篇关于学生信息表格提供错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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