[已解决] C#中的CRUD操作 [英] [Solved] CRUD operation in C#

查看:281
本文介绍了[已解决] C#中的CRUD操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我坚持使用google seach来找出执行此操作的简单方法.

1)从C#代码运行"SELECT ID,Name FROM Member WHERE ID = 12",并将ID和Name的值导出到两个单独的变量(Int_Id,Str_Name)中.
2)使用某些条件为值更新表.
3)在某些情况下从表中删除行.
4)将一个记录插入到表中.与从C#中的executinn"INSERT INTO ...."查询相同.

请引导我.我从Google向我提供的搜索结果中尝试了此方法.
但它是使用Adaptot,command,reader,dataset的日志方式.

Hello All,

I stuck up with google seach to find out what is the simple way to do this.

1) Run "SELECT ID,Name FROM Member WHERE ID=12" from C# code and export value of ID and Name into two separate variable (Int_Id, Str_Name).
2) Update a table for a value using some condition.
3) Delete a rows from table for some condition.
4) Insert one record into a table . same like executinn "INSERT INTO ...." query from C#.

Please guid me. I tried this from search result what google has provided to me.
but its a log way of using adaptot , command , reader , dataset.

推荐答案

在这里,从根本上讲,这是您想要处理在以下位置执行SQL方面的所有知识使用C#的SqlServer.阅读评论并尝试理解.

此示例仅具有一个选择SQL,对于其他SQL(例如"DELETE,INSERT,CREATE TABLE,UPDATE"),您要做的就是将CommandText更改为要执行的SQL,并相应地选择适当的方法. :)
Here goes, this is basically everything you want to know in terms of handling executing SQL on SqlServer using C#. Read through the comments and try to understand.

This example only has a select SQL, for other SQL like "DELETE, INSERT, CREATE TABLE, UPDATE" all you have to do is change the CommandText to whatever the SQL you want to execute and choose the appropriate method accordingly. :)
// This is your connection to your database
using (SqlConnection connection = new SqlConnection())
{
    connection.ConnectionString = "Put your connection string here";

    // This creates an object with which you can execute sql
    using (SqlCommand command = connection.CreateCommand())
    {
        command.CommandText = "SELECT COUNT(*) FROM TestTable WHERE TestId = @TestId";
        command.CommandType = CommandType.Text;
        // If the SQL you want to execute is a stored procedure, use the commented 2 lines below
        // instead of the 2 lines above.
        // command.CommandText = "SP_GetTestSubjectCount_Select";
        // command.CommandType = CommandType.StoredProcedure;

        // This is how you add a parameter to your sql command
        // This way you are protected against SQL injection attacks
        SqlParameter testIdParameter = command.CreateParameter();
        testIdParameter.ParameterName = "@TestId";
        testIdParameter.Value = "Some Value";
        command.Parameters.Add(testIdParameter);

        try
        {

            connection.Open();

            // This is the easiest way to iterate through the returned results with MULTIPLE rows
            // This way can ONLY be used for SELECT statements or SPs which returns results
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader.GetInt32(0));
            }

            // If your SQL returns just ONE VALUE you can use the code below
            int testObjectCount = (int)command.ExecuteScalar();

            // For DELETE, UPDATE, INSERT statements which does not return a resultset
            // instead they return the number of records affected
            // Use the code below for that instead of the above code
            int affectedRows = command.ExecuteNonQuery();

        }
        finally
        {
            try
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
            catch
            {
                // This catch block suppresses any errors occured while closing the connection
                // remove the try catch block if you want your application to know of this exception as well
            }
        }
    }
}



我知道我只是在给你代码,但是却让我很讨厌,但是今天我在工作中很无聊,所以认为这是一个很好的手势:D只是在这个时候.不要咬指甲,不会再发生:D

希望这可以帮助.问候:)



I know I am spoiling you with just giving you the code, but I am way bored at work today, so consider this as a good gesture :D Just for this time. Don''t bite your nails, won''t happen again :D

Hope this helps. Regards :)


我建​​议您使用Google SqlConnectionSqlCommand.基本上,您首先创建了与数据库的连接,并用于在服务器上执行命令.
选择所需的SqlCommand 的方法是ExecuteReader.对于其余的"CUD"操作,您将需要使用ExecuteNonQuery不要忘记在完成连接后关闭连接,并保护自己免受SQL Injection攻击.
[
I suggest you Google SqlConnection and SqlCommand. You basically create a connection to the database with the first and use to execute the command on the server.
The method for selecting SqlCommand you want is ExecuteReader. For the remaining "CUD" operations you will need to use ExecuteNonQuery Don''t forget to close the connection when done, and protect yourself from SQL Injection attacks.
This[^] should get you started.


这篇关于[已解决] C#中的CRUD操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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