我想数据插入到使用C#中的SQL Server DATABSE [英] I want to insert data into a SQL Server databse using C#

查看:228
本文介绍了我想数据插入到使用C#中的SQL Server DATABSE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想数据插入到使用C#SQL Server数据库。

有在我的code没有错误。但是,输入数据后我遍历。它让我看到的数据。但是,当我打开数据库表。我无法看到插入的数据。我初始化表中创建2行的时间。但是,当我通过遍历窗体我不能看到这些行。

这么多的审理后一个错误。我看到,数据库连接发生故障时,每当我运行它。我看到了绿色插头的数据库图标到红十字点击F5之后。那是什么原因呢?如果它是那么应该是什么解决办法吗?

大家好,请帮助我。我是新的C#。

这是 insert.cs

 使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms的;命名空间employeDB1
{
公共部分类插入:表
{
    公共插入()
    {
        的InitializeComponent();
    }    的DatabaseConnection objConnect;
    串conString;
    INT INC;
    INT MAXROWS;
    DataSet的DS =新的DataSet();
    DataRow的卓尔;    公共无效Insert_Load(对象发件人,EventArgs的发送)
    {
        尝试
        {
            objConnect =新的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();
        }
        赶上(例外错误)
        {
            MessageBox.Show(err.Message);
        }
    }    私人无效的button1_Click(对象发件人,EventArgs的发送)
    {
            行的DataRow = ds.Tables [0] .NewRow();
            行[0] = int.Parse(textBox1.Text);
            行[1] = textBox2.Text;
            行[2] = textBox3.Text;
            行[3] = int.Parse(textBox4.Text);
            ds.Tables [0] .Rows.Add(行);
        尝试
        {            objConnect.UpdateDatabase(DS);
            MAXROWS = MAXROWS + 1;
            INC = MAXROWS - 1;
            MessageBox.Show(已插入记录);        }
        赶上(例外错误)
        {
            MessageBox.Show(err.Message);
        }
    }    私人无效NavigateRecords()
    {
        卓尔= 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();
    }    私人无效button2_Click(对象发件人,EventArgs的发送)
    {
        如果(INC大于0)
        {
            inc--;
            NavigateRecords();        }
        其他
        {
            的MessageBox.show(第一条);
        }
    }
 }
}

这是数据库连接类:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.Threading.Tasks;命名空间employeDB1
{
一流的DatabaseConnection
{
    私人字符串sql_string;
    私人字符串STRCON;
    System.Data.SqlClient.SqlDataAdapter da_1;    公共字符串的Sql
    {
        集合{sql_string =价值; }
    }    公共字符串connection_string
    {
        集合{STRCON =价值; }
    }    公共System.Data.DataSet中的getConnection
    {
        {返回MyDataSet(); }
    }    私人System.Data.DataSet中MyDataSet()
    {
        System.Data.SqlClient.SqlConnection CON =新System.Data.SqlClient.SqlConnection(STRCON);
        con.Open();
        da_1 =新System.Data.SqlClient.SqlDataAdapter(sql_string,CON);
        System.Data.DataSet中dat_set =新System.Data.DataSet中();
        da_1.Fill(dat_setTable_Data_1);
        con.Close();
        返回dat_set;
    }    公共无效UpdateDatabase(System.Data.DataSet中DS)
    {
        System.Data.SqlClient.SqlCommandBuilder CB =新System.Data.SqlClient.SqlCommandBuilder(da_1);
        cb.DataAdapter.Update(ds.Tables [0]);
    }
}


解决方案

尽量把你的连接字符串中的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..

这篇关于我想数据插入到使用C#中的SQL Server DATABSE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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