简单表单应用程序与Db连接C# [英] Simple formApplication With Db connection C#

查看:53
本文介绍了简单表单应用程序与Db连接C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!

我是C#和genreal编程的初学者,为了更好地学习它,我创建了一个注册客户的表单应用程序.

最初,我是通过尝试了解OOP来完成此工作的,现在我直接去研究DB方面的内容.
我想将信息插入数据库,但得到以下信息:

无效的实例"

插入到Db的代码如下所示:

Hi!

Im kind of a beginner to C# and programming in genreal and in order to get better at it i created a forms application that registers customers.

At first i did this by trying to learn about OOP and now im going to the DB stuff.

I want to insert info to the database but i get the following:

"invalid instance"

The code for inserting to Db looks like this:

//String ConnectionString = ;
        SqlConnection Cn = new SqlConnection(@"Data Source=ADMIN-PC\\MasterDB;Initial Catalog=Test;Integrated Security=True");
        SqlCommand Command = new SqlCommand();
        SqlDataReader DataReader;

public void InsertInfo(string _valueOne, string _ValueTwo, string _valueThree, string _valuefour, string valueFive, string valuesix, string valueseven,
           string valueeight, string valuenine, string valueten)
       {
           try
           {

               Cn.Open();
               Command.Connection = Cn;

               Command.CommandText = @"INSERT INTO CustomerInfo(Firstname,LastName,HoneNr,WorkNr,BusinessMail,PrivateMail,Street,City,Zipcode,Country)
           @VALUES('" + _valueOne + "', '" + _ValueTwo + "','" + _valueThree + "','" + _valuefour + "','" + valueFive + "','" + valuesix + "', '" + valueseven + "','" + valueeight + "','" + valuenine + "','" + valueten + "')";

               Command.ExecuteNonQuery();
               Cn.Close();
               MessageBox.Show("infor inserted");
           }

           catch (Exception ObjException)
           {
               throw ObjException;
           }
}



谈到cn.open()时,它立即崩溃,我真的不明白问题出在哪里吗?

我也想从Db获取数据并将其添加到gridview

到目前为止,此代码如下所示:



As soon as it comes to cn.open() it crashes and i don´t really understand what the problem is?

Also i want to get data from the Db and add this to a gridview

The code so far for this is like this:

public void GetInfo()
{
    Cn.Open();
    Command.CommandText = "SELECT * FROM CustomerInfo";
    DataReader = Command.ExecuteReader();

    if (DataReader.HasRows)
    {
        while (DataReader.Read())
        {

        }
    }
}



我有点卡在这里,因为我不知道如何返回数据,我应该在列表中返回它吗?另外,如何将其添加到gridview?

我是一个初学者,我在这里待了几个小时,所以如果这真的很简单,请耐心等待我!

谢谢!



Im kind of stuck here since i don´t know how to return the data, should i return it in a list? Also how do i add this to the gridview?

im a beginner and ive been at this for a few hours so if this is really simple for you be patient with me ill get there!

thanks!

推荐答案

这是一个可怕的想法.作为初学者,您应该在控制台上学习C#,然后在winforms上学习,然后可能开始担心学习SQL等问题.您所咬的东西超出了您的咀嚼能力,因此您不太可能会学到很多东西.话虽如此,该网站不仅涉及问题,还拥有丰富的文章库,如果您要搜索这些文章,则会找到所需的所有答案.

理想情况下,您会将数据作为强类型对象的集合返回,但实际上,所有数据代码的设计都欠佳,这仅仅是因为您又想一次学习太多的东西.学习合理地做好一件事,然后再开始做另一件事.不要试图一次全部学习.
This is a horrible idea. As a beginner, you should learn C# on the console, then winforms and then perhaps start to worry about learning SQL and so on. You''re biting off more than you can chew, and it''s unlikely you''ll learn much as a result. Having said that, this site is not just about questions, it has a rich library of articles and if you were to search those, you''d find all the answers you need.

Ideally you''d return your data as a collection of a strongly typed object, but really, all your data code is poorly designed, simply because, again, you''re trying to learn too many things at once. Learn to do one thing reasonably well, then start on the next. Don''t try to learn it all at once.


这篇关于简单表单应用程序与Db连接C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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