2023年6月21日发(作者:)
C#查询和插⼊数据到sql数据库连接sql数据库步骤:1. vs10 -> Tools -> Connect to Database ->Microsoft SQL Server -> 填写Server name : Use SQL Server Authentication (常⽤⽅式):Select or enter a database name -> OK2. 在Server Explorer 中选择连到的数据库右键选属性获取连接字符串。3. 在webconfig中创建AppSetting节点key为connectionAWGDB值为连接字符串private SqlConnection _getConnection() //得到连接对象{try { string connectionString = tings["connectionAWGDB"].ToString(); SqlConnection connectionAWGDB = new SqlConnection(connectionString); return connectionAWGDB;}catch(Exception exception){ return null;}}private SqlCommand _getCommand(string sqlString, SqlConnection sqlConnection) //得到sql命令对象{ SqlCommand sqlCommand = new SqlCommand(sqlString, sqlConnection); return sqlCommand;}public string GetUIDInUsers(string userName) //根据⽤户名查找⽤户ID
{ string uID = ""; string selectSql = ("select UID from Users where UserName='{0}'", userName); SqlConnection sqlConnection = _getConnection(); (); object reader = _getCommand(selectSql, sqlConnection).ExecuteScalar(); if (reader != null) { uID = ng(); } (); return uID;
}private void _writeLogs(...Parameters){ // 传⼊参数 SqlParameter[] prams={ new SqlParameter("@LogTypeID",logTypeID),
new SqlParameter("@LoginTime",loginTime), new SqlParameter("@UID",userID), new SqlParameter("@FormTypeID",formTypeID), ...
}; Log(prams);}public void InsertLog(SqlParameter[] parm) //插⼊数据{try{
string insertSql = ("insert into ormation(LogTypeID,LoginTime,UID, FormTypeID,ClientID,)values({0},'{1}','{2}',{3},...)", parm[0].Value, parm[1].Value, parm[2].Value, parm[3].Value,...); SqlConnection sqlConnection = _getConnection(); (); _getCommand(insertSql, sqlConnection).ExecuteNonQuery(); _getConnection().Close(); } catch (Exception exception) { throw xception(rentMethodName(), exception); }}
2023年6月21日发(作者:)
C#查询和插⼊数据到sql数据库连接sql数据库步骤:1. vs10 -> Tools -> Connect to Database ->Microsoft SQL Server -> 填写Server name : Use SQL Server Authentication (常⽤⽅式):Select or enter a database name -> OK2. 在Server Explorer 中选择连到的数据库右键选属性获取连接字符串。3. 在webconfig中创建AppSetting节点key为connectionAWGDB值为连接字符串private SqlConnection _getConnection() //得到连接对象{try { string connectionString = tings["connectionAWGDB"].ToString(); SqlConnection connectionAWGDB = new SqlConnection(connectionString); return connectionAWGDB;}catch(Exception exception){ return null;}}private SqlCommand _getCommand(string sqlString, SqlConnection sqlConnection) //得到sql命令对象{ SqlCommand sqlCommand = new SqlCommand(sqlString, sqlConnection); return sqlCommand;}public string GetUIDInUsers(string userName) //根据⽤户名查找⽤户ID
{ string uID = ""; string selectSql = ("select UID from Users where UserName='{0}'", userName); SqlConnection sqlConnection = _getConnection(); (); object reader = _getCommand(selectSql, sqlConnection).ExecuteScalar(); if (reader != null) { uID = ng(); } (); return uID;
}private void _writeLogs(...Parameters){ // 传⼊参数 SqlParameter[] prams={ new SqlParameter("@LogTypeID",logTypeID),
new SqlParameter("@LoginTime",loginTime), new SqlParameter("@UID",userID), new SqlParameter("@FormTypeID",formTypeID), ...
}; Log(prams);}public void InsertLog(SqlParameter[] parm) //插⼊数据{try{
string insertSql = ("insert into ormation(LogTypeID,LoginTime,UID, FormTypeID,ClientID,)values({0},'{1}','{2}',{3},...)", parm[0].Value, parm[1].Value, parm[2].Value, parm[3].Value,...); SqlConnection sqlConnection = _getConnection(); (); _getCommand(insertSql, sqlConnection).ExecuteNonQuery(); _getConnection().Close(); } catch (Exception exception) { throw xception(rentMethodName(), exception); }}
发布评论