且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我想数据插入到使用C#中的SQL Server DATABSE

更新时间:2023-01-29 09:57:58

尽量把你的连接字符串中的app.config文件,然后检索它,或者你可以硬code在你insert.cs文件。我一直在使用app.config文件检查,它是工作。在这里,我做了什么。


  

app.config文件


块引用>
 <?XML版本=1.0编码=UTF-8&GT?;
<结构>
  <&是connectionStrings GT;
    <添加名称=的ConnectionString的connectionString =服务器= 192.168.8.295;数据库= TESTDB;
           UID =塔潘; PWD =库马尔;/>
  < /&是connectionStrings GT;
< /结构>

然后,只需更换您从设置在insert.cs页retriving的东西。

  conString = ConfigurationManager.ConnectionStrings [的connectionString]的ToString()。

和删除这个事情

  objConnect.Sql = Properties.Settings.Default.SQL;

和检查一次,如果是工作或没有。对我来说这是工作。

不过,如果你有问题,那么首先将尝试捕获在code和调试彻底第一,看到你的code是死亡。

I want to insert data into a SQL Server database using C#.

There is no error in my code. But after entering the data I traverse. It shows me the data. But when I open the database table. I can't see the inserted data. I initialize the table in the time of creation with 2 rows. But when I traverse through form I can't see those rows.

After so many trial an error. I see that database connection breaks down whenever I run it. I saw that the "green plug" on the database icon went to "red cross" after clicking F5. Is that the reason? If it is then what should be the solution ?

Guys please help me. I am new at C#.

This is insert.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace employeDB1
{
public partial class Insert : Form
{
    public Insert()
    {
        InitializeComponent();
    }

    DatabaseConnection objConnect;
    string conString;
    int inc ;
    int MaxRows;
    DataSet ds = new DataSet();
    DataRow dRow;

    public void Insert_Load(object sender, EventArgs e)
    {
        try
        {
            objConnect = new DatabaseConnection();
            conString = Properties.Settings.Default.EmployeeConnectionString;
            objConnect.connection_string = conString;
            objConnect.Sql = Properties.Settings.Default.SQL;
            ds = objConnect.GetConnection;
            MaxRows = ds.Tables[0].Rows.Count;
            inc = MaxRows - 1;
            //NavigateRecords();
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
            DataRow row = ds.Tables[0].NewRow();
            row[0] = int.Parse(textBox1.Text);
            row[1] = textBox2.Text;
            row[2] = textBox3.Text;
            row[3] = int.Parse(textBox4.Text);
            ds.Tables[0].Rows.Add(row);
        try
        {

            objConnect.UpdateDatabase(ds);
            MaxRows = MaxRows + 1;
            inc = MaxRows - 1;
            MessageBox.Show("RECORD INSERTED");

        }
        catch(Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }

    private void NavigateRecords()
    {
        dRow = ds.Tables[0].Rows[inc];
        textBox1.Text = dRow.ItemArray.GetValue(0).ToString();
        textBox2.Text = dRow.ItemArray.GetValue(1).ToString();
        textBox3.Text = dRow.ItemArray.GetValue(2).ToString();
        textBox4.Text = dRow.ItemArray.GetValue(3).ToString();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (inc>0)
        {
            inc--;
            NavigateRecords();

        }
        else
        {
            MessageBox.Show("First Record");
        }
    }
 }
}

This is the database connection class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace employeDB1
{
class DatabaseConnection
{
    private string sql_string;
    private string strCon;
    System.Data.SqlClient.SqlDataAdapter da_1;

    public string Sql
    {
        set { sql_string = value; }
    }

    public string connection_string
    {
        set { strCon = value; }
    }

    public System.Data.DataSet GetConnection
    {
        get { return MyDataSet(); }
    }

    private System.Data.DataSet MyDataSet()
    {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon);
        con.Open();
        da_1 = new System.Data.SqlClient.SqlDataAdapter(sql_string, con);
        System.Data.DataSet dat_set = new System.Data.DataSet();
        da_1.Fill(dat_set, "Table_Data_1");
        con.Close();
        return dat_set;
    }

    public void UpdateDatabase(System.Data.DataSet ds)
    {
        System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder(da_1);
        cb.DataAdapter.Update(ds.Tables[0]);
    }
}

try to put your connection string in a app.config file and then retrieve it or you can hard code it in your insert.cs file. I have checked it using app.config file and it was working. Here what I did.

app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="connectionString" connectionString="Server=192.168.8.295;Database=TestDB;
           uid=Tapan;pwd=kumar;"/>
  </connectionStrings>
</configuration>

Then just replace the thing you are retriving from settings in your insert.cs page

 conString = ConfigurationManager.ConnectionStrings["connectionString"].ToString();

And remove this thing

 objConnect.Sql = Properties.Settings.Default.SQL;

And check once if it is working or not. For me it was working.

Still if you have problem then first put try catch in your code and debug thoroughly first and see where your code is dying..