如何使用控制台应用程序写回SQL数据库 [英] How to write back into a SQL database using Console Application

查看:149
本文介绍了如何使用控制台应用程序写回SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下代码从SQL数据库中读取,现在我想学习如何写入数据库。





 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Data.SqlClient;

命名空间 ReportOrders
{
class 报告
{
静态 void Main( string [] args)
{
SqlConnection dataConn = new SqlConnection();

尝试
{
dataConn.ConnectionString =
Integrated Security = true; Initial Catalog = AdventureWorks; +
数据源=。;
dataConn.Open();
Console.Write( 请输入经理ID);
string EmployeeID = Console.ReadLine();
SqlCommand dataComm = new SqlCommand();
dataComm.Connection = dataConn;
dataComm.CommandText =
SELECT EmployeeID,ContactID,Title,BirthDate +
FROM HumanResources.Employee WHERE ManagerID =' + EmployeeID + ';
Console.WriteLine( 即将执行:{0} \ n\\\
,dataComm.CommandText);
SqlDataReader dataReader = dataComm.ExecuteReader();
while (dataReader.Read())
{
int ContactID = dataReader.GetInt32( 0 );
string Title = dataReader.GetString( 2 );
int BirthDate = dataReader.GetInt32( 1 );
Console.WriteLine(
EmployeeID:{0} \ nID:{1} \\ \\ n {2} \ n {3} \ n,EmployeeID,ContactID,Title,BirthDate);
}
dataReader.Close();
}
catch (SqlException e)
{
Console.WriteLine( 访问数据库时出错:{0},e.Message);

}
最后
{
dataConn.Close();
}
}
}
}

解决方案

用于在DB中写回,你需要更新数据库,如果你不知道这个例子,你可以去....

http://www.youtube.com/watch?v=yh0A9rGXp1M [ ^ ]

http://www.daniweb.com/software-development/csharp/code/290540/sql-databases-using-c-connecting-and-updating [ ^ ]

I can read from SQL database using the following code, now I want to learn how to write into a database.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ReportOrders
{
    class Report
    {
        static void Main(string[] args)
        {
            SqlConnection dataConn = new SqlConnection();

            try
            {
                dataConn.ConnectionString =
                    "Integrated Security=true;Initial Catalog=AdventureWorks;" +
                    "Data Source=.";
                dataConn.Open();
                Console.Write("Please enter a Manager ID ");
                string EmployeeID = Console.ReadLine();
                SqlCommand dataComm = new SqlCommand();
                dataComm.Connection = dataConn;
                dataComm.CommandText =
                    "SELECT EmployeeID, ContactID,Title, BirthDate " +
                    "FROM HumanResources.Employee WHERE ManagerID='" + EmployeeID + "'";
                Console.WriteLine("About to execute: {0}\n\n", dataComm.CommandText);
                SqlDataReader dataReader = dataComm.ExecuteReader();
                while (dataReader.Read())
                {
                    int ContactID = dataReader.GetInt32(0);
                    string Title = dataReader.GetString(2);
                    int BirthDate = dataReader.GetInt32(1);                   
                    Console.WriteLine(
                        "EmployeeID: {0}\nID: {1}\n{2}\n{3}\n", EmployeeID, ContactID, Title, BirthDate);
                }
                dataReader.Close();
            }
            catch (SqlException e)
            {
                Console.WriteLine("Error accessing the database: {0}", e.Message);

            }
            finally
            {
                dataConn.Close();
            }
        }
    }
}

解决方案

For writing back in DB, you need to udpate DB, If you''re not aware of this kinda example, here you go....
http://www.youtube.com/watch?v=yh0A9rGXp1M[^]
http://www.daniweb.com/software-development/csharp/code/290540/sql-databases-using-c-connecting-and-updating[^]


这篇关于如何使用控制台应用程序写回SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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