2023年6月21日发(作者:)
c#数据库操作读写数据库本⽂简单的介绍的通过C#对数据库的读写操作。
请先参考⼀下代码:using System;using c;using ;using ;using ;using ent;using ;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { string connectonString = "Data Source=(local);Initial Catalog=test; Integrated Security=SSPI;"; string commandString = "insert into t values ('100002','wei song',22,'3','Liu','test from sql')"; string commansString_Parameter = "insert into t values (@id,@name,22,'3','Liu','test from sql')"; string commansString_Parameter_ext = "insert into t values (@id_ext,@name_ext,22,'3','Liu','test from sql')"; SqlConnection sqlConnection = new SqlConnection(connectonString); (); ine(Version); ine(); // write the sql command , use sqlcommand to insert data to the datebase. SqlCommand command = new SqlCommand(); tion = sqlConnection; dText = commandString; eNonQuery(); ine(); // add parameters to the sql command ine("please input user id & user name"); string id = ne(); string name = ne(); dText = commansString_Parameter; SqlParameter sqlParameter_id = new SqlParameter("@id",id); SqlParameter sqlParameter_name = new SqlParameter("@name",name); (sqlParameter_id); (sqlParameter_name); eNonQuery(); // another way to add sql parameters ine("please input user id & user name"); id = ne(); name = ne(); dText = commansString_Parameter_ext; SqlParameter sqlParameter_id_ext = new SqlParameter("@id_ext",,10); SqlParameter sqlParameter_name_ext = new SqlParameter("@name_ext",,50); sqlParameter_id_ = id; sqlParameter_name_ = name; (sqlParameter_id_ext); (sqlParameter_name_ext); eNonQuery(); // read data from database string readDataFormDatabase_string = "select * from t"; SqlCommand readDataCommand = new SqlCommand(); dText = readDataFormDatabase_string; tion = sqlConnection; SqlDataReader sqlDataReader = eReader(); ine("//"); ine("id name age class mentor infor "); while(()) { string user_id = ing(0); string user_name = ing(1); int user_age = 32(2); string user_class = ing(3); string user_mentor = ing(4); string user_info = ing(5); // there will be filled by blank after the name, for the name is 50 length in the database. need to process the user_name ine(user_id+user_name+user_age+user_class+user_mentor+user_info); } (); } }}
string commansString_Parameter = "insert into t values (@id,@name,22,'3','Liu','test from sql')";这⾥是定义个⽤于通过参数进⾏数据库插⼊的语句,我们程序中经常需要给数据库中插⼊特定的参数信息。添加参数: SqlParameter sqlParameter_id = new SqlParameter("@id",id);将这个参数添加到SqlCommand中: (sqlParameter_id);详细说明在连接数据库对数据库进⾏操作之前,需要建⽴⼀个对数据库的连接: SqlConnection sqlConnection = new SqlConnection(connectonString);connectionString是⼀个已经定义好的字符串,存储着关于数据库连接的相关信息。 string connectonString = "Data Source=(local);Initial Catalog=test; Integrated Security=SSPI;";Data Source=(local) 指定我们要连接的数据库的位置,此例为连接本地数据库。Initial Catalog=test 指定要连接数据库服务器中的哪个数据库,在本例中建⽴了⼀个名为test的数据库。Integrated Security=SSPI 指定了数据库的访问⽅式,本例是通过SSPI指定为通过windows进⾏⾝份验证。同事,数据库连接字符串中还可以有多个其他的参数可选,如下:详情请查看MSDN. 当然,很多情况下我们需要访问其他电脑上的数据库,这样就需要User IDPassword等属性。
定义好数据库连接后,通过Open()函数打开对数据库的访问:();
对数据库进⾏操作的时候需要⽤SqlCommand类,并指定这个SqlCommand对象所对应的数据库连接和SQL语句: tion = sqlConnection; dText = commandString;
如果是给数据库中插⼊数据, 则执⾏:
eNonQuery();通常情况下,我们要把⽤户输⼊的数据添加到数据库中,这个时候⽐较⽅便的操作就是通过SqlParameter给Sql语句中添加参数,这样就不⽤在⾃⼰合成查询字符串了。如下所⽰:ine("please input user id & user name");string id = ne();string name = ne();dText = commansString_Parameter;SqlParameter sqlParameter_id = new SqlParameter("@id",id);SqlParameter sqlParameter_name = new SqlParameter("@name",name);(sqlParameter_id);(sqlParameter_name);eNonQuery();读取数据库的数据的时候,我们通过SqlDataReader来获取数据库的信息SqlDataReader sqlDataReader = eReader();这⾥的SqlDataReader对象获取的是查询返回的所有信息,通过Read()⽅法读取每⼀⾏的返回信息: while(()){ string user_id = ing(0); string user_name = ing(1); int user_age = 32(2); string user_class = ing(3); string user_mentor = ing(4); string user_info = ing(5); // there will be filled by blank after the name, for the name is 50 length in the database. need to process the user_name ine(user_id+user_name+user_age+user_class+user_mentor+user_info);}获取不同的列的信息,通过GetXXX()来获取,括号中的数字表⽰获取的是第⼏列的数据。
2023年6月21日发(作者:)
c#数据库操作读写数据库本⽂简单的介绍的通过C#对数据库的读写操作。
请先参考⼀下代码:using System;using c;using ;using ;using ;using ent;using ;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { string connectonString = "Data Source=(local);Initial Catalog=test; Integrated Security=SSPI;"; string commandString = "insert into t values ('100002','wei song',22,'3','Liu','test from sql')"; string commansString_Parameter = "insert into t values (@id,@name,22,'3','Liu','test from sql')"; string commansString_Parameter_ext = "insert into t values (@id_ext,@name_ext,22,'3','Liu','test from sql')"; SqlConnection sqlConnection = new SqlConnection(connectonString); (); ine(Version); ine(); // write the sql command , use sqlcommand to insert data to the datebase. SqlCommand command = new SqlCommand(); tion = sqlConnection; dText = commandString; eNonQuery(); ine(); // add parameters to the sql command ine("please input user id & user name"); string id = ne(); string name = ne(); dText = commansString_Parameter; SqlParameter sqlParameter_id = new SqlParameter("@id",id); SqlParameter sqlParameter_name = new SqlParameter("@name",name); (sqlParameter_id); (sqlParameter_name); eNonQuery(); // another way to add sql parameters ine("please input user id & user name"); id = ne(); name = ne(); dText = commansString_Parameter_ext; SqlParameter sqlParameter_id_ext = new SqlParameter("@id_ext",,10); SqlParameter sqlParameter_name_ext = new SqlParameter("@name_ext",,50); sqlParameter_id_ = id; sqlParameter_name_ = name; (sqlParameter_id_ext); (sqlParameter_name_ext); eNonQuery(); // read data from database string readDataFormDatabase_string = "select * from t"; SqlCommand readDataCommand = new SqlCommand(); dText = readDataFormDatabase_string; tion = sqlConnection; SqlDataReader sqlDataReader = eReader(); ine("//"); ine("id name age class mentor infor "); while(()) { string user_id = ing(0); string user_name = ing(1); int user_age = 32(2); string user_class = ing(3); string user_mentor = ing(4); string user_info = ing(5); // there will be filled by blank after the name, for the name is 50 length in the database. need to process the user_name ine(user_id+user_name+user_age+user_class+user_mentor+user_info); } (); } }}
string commansString_Parameter = "insert into t values (@id,@name,22,'3','Liu','test from sql')";这⾥是定义个⽤于通过参数进⾏数据库插⼊的语句,我们程序中经常需要给数据库中插⼊特定的参数信息。添加参数: SqlParameter sqlParameter_id = new SqlParameter("@id",id);将这个参数添加到SqlCommand中: (sqlParameter_id);详细说明在连接数据库对数据库进⾏操作之前,需要建⽴⼀个对数据库的连接: SqlConnection sqlConnection = new SqlConnection(connectonString);connectionString是⼀个已经定义好的字符串,存储着关于数据库连接的相关信息。 string connectonString = "Data Source=(local);Initial Catalog=test; Integrated Security=SSPI;";Data Source=(local) 指定我们要连接的数据库的位置,此例为连接本地数据库。Initial Catalog=test 指定要连接数据库服务器中的哪个数据库,在本例中建⽴了⼀个名为test的数据库。Integrated Security=SSPI 指定了数据库的访问⽅式,本例是通过SSPI指定为通过windows进⾏⾝份验证。同事,数据库连接字符串中还可以有多个其他的参数可选,如下:详情请查看MSDN. 当然,很多情况下我们需要访问其他电脑上的数据库,这样就需要User IDPassword等属性。
定义好数据库连接后,通过Open()函数打开对数据库的访问:();
对数据库进⾏操作的时候需要⽤SqlCommand类,并指定这个SqlCommand对象所对应的数据库连接和SQL语句: tion = sqlConnection; dText = commandString;
如果是给数据库中插⼊数据, 则执⾏:
eNonQuery();通常情况下,我们要把⽤户输⼊的数据添加到数据库中,这个时候⽐较⽅便的操作就是通过SqlParameter给Sql语句中添加参数,这样就不⽤在⾃⼰合成查询字符串了。如下所⽰:ine("please input user id & user name");string id = ne();string name = ne();dText = commansString_Parameter;SqlParameter sqlParameter_id = new SqlParameter("@id",id);SqlParameter sqlParameter_name = new SqlParameter("@name",name);(sqlParameter_id);(sqlParameter_name);eNonQuery();读取数据库的数据的时候,我们通过SqlDataReader来获取数据库的信息SqlDataReader sqlDataReader = eReader();这⾥的SqlDataReader对象获取的是查询返回的所有信息,通过Read()⽅法读取每⼀⾏的返回信息: while(()){ string user_id = ing(0); string user_name = ing(1); int user_age = 32(2); string user_class = ing(3); string user_mentor = ing(4); string user_info = ing(5); // there will be filled by blank after the name, for the name is 50 length in the database. need to process the user_name ine(user_id+user_name+user_age+user_class+user_mentor+user_info);}获取不同的列的信息,通过GetXXX()来获取,括号中的数字表⽰获取的是第⼏列的数据。
发布评论