📄 ado.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace SQServer
{
class ADO
{
static SqlConnection conn = new SqlConnection();
static string cs = @"Data Source=itx;
Initial Catalog=SQ;
Persist Security Info=True;
User ID=sa;Password=";
public static void ado()
{
conn.ConnectionString = cs;
}
//注册
public static int Registation(string[] str) {
/*假设有一存储过程
create procedure Registration
(
@uid int output, @name varchar(16),@password varchar(16),@picture int
)*/
try
{
SqlCommand cmd=new SqlCommand(Registration,conn);
cmd.CommandType=CommandType.StoredProcedure;//告知执行存储过程
SqlParameter uid=new SqlParameter("@uid",SqlDbType.Int,4);
uid.Direction=ParameterDirection.Output;//指定该参数为输出参数
cmd.Parameters.Add(uid);//将参数添加到命令对象
SqlParameter name = new SqlParameter("@name", SqlDbType.VarChar, 16);
name.Value = str[1];
cmd.Parameters.Add(name);//将参数添加到命令对象
SqlParameter password = new SqlParameter("@password", SqlDbType.VarChar, 16);
password.Value = str[2];
cmd.Parameters.Add(password);//将参数添加到命令对象
SqlParameter picture = new SqlParameter("@picture", SqlDbType.Int, 4);
picture.Value = str[3];
cmd.Parameters.Add(picture);//将参数添加到命令对象
conn.Open();//连接通数据库
cmd.ExecuteNonQuery();//执行SQL语句并反回影响行数
int id=cmd.Parameters["@uid"].Value;//此为获取返回的输出参数值
return id;
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
finally
{
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
}
//登录
public static User Registers(string[] str)
{
try
{
User u = new User();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = System.Data.CommandType.Text;
comm.CommandText = @"select * from user where id=" + str[1] + " Password='"+str[2]+"'";
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
u.Uid = reader.GetInt16(0);
u.Name = reader.GetString(1);
u.Picture=reader.GetInt16();
//u.Sex=reader
if (!reader.IsDBNull(2))
{
s.Gpa = reader.GetDecimal(2);
}
us(s);
}
comm.CommandText = @"select * from Friend where id=" + u.Uid;
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
User fu = new User();
fu.Uid = reader.GetInt16(0);
fu.Name = reader.GetString(1);
fu.Picture = reader.GetInt16();
//u.Sex=reader
if (!reader.IsDBNull(2))
{
fu.Gpa = reader.GetDecimal(2);
}
u.Friend.Add(fu);
}
return u;
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
finally
{
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
}
//查找好友(ID)
public static User asdf(string id){
try
{
User u = new User();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = System.Data.CommandType.Text;
comm.CommandText = @"select * from user where id=" + id ;
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
{
u.Uid = reader.GetInt16(0);
u.Name = reader.GetString(1);
u.Picture = reader.GetInt16();
//u.Sex=reader
if (!reader.IsDBNull(2))
{
s.Gpa = reader.GetDecimal(2);
}
}
return u;
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
finally
{
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
}
//查找好友(在线)
public static List<User> asdf2()
{
try
{
List<User> us = new List<User>();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandType = System.Data.CommandType.Text;
comm.CommandText = @"select * from user where id=" + str[1] + " Password='" + str[2] + "'";
conn.Open();
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
{
User u = new User();
u.Uid = reader.GetInt16(0);
u.Name = reader.GetString(1);
u.Picture = reader.GetInt16();
//u.Sex=reader
if (!reader.IsDBNull(2))
{
s.Gpa = reader.GetDecimal(2);
}
us(s);
}
return us;
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
finally
{
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
}
//增加好友
public static int adsdd(string[] str) {
try
{
SqlCommand cmd = new SqlCommand(Registration, conn);
cmd.CommandType = CommandType.StoredProcedure;//告知执行存储过程
SqlParameter uid = new SqlParameter("@uid", SqlDbType.Int, 4);
uid.Value = str[1];
cmd.Parameters.Add(uid);//将参数添加到命令对象
SqlParameter fid = new SqlParameter("@fid", SqlDbType.Int, 4);
fid.Value = str[2];
cmd.Parameters.Add(fid);//将参数添加到命令对象
conn.Open();//连接通数据库
int i=cmd.ExecuteNonQuery();//执行SQL语句并反回影响行数
return i;
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
finally
{
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
}
//删除好友
public static int asdfas(string[] str) {
try
{
SqlCommand cmd = new SqlCommand(Registration, conn);
cmd.CommandType = CommandType.StoredProcedure;//告知执行存储过程
SqlParameter uid = new SqlParameter("@uid", SqlDbType.Int, 4);
uid.Value = str[1];
cmd.Parameters.Add(uid);//将参数添加到命令对象
SqlParameter fid = new SqlParameter("@fid", SqlDbType.Int, 4);
fid.Value = str[2];
cmd.Parameters.Add(fid);//将参数添加到命令对象
conn.Open();//连接通数据库
int i = cmd.ExecuteNonQuery();//执行SQL语句并反回影响行数
return i;
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
finally
{
if (conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -