升级

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LpSpace;
using System.IO;
using System.Data.SqlClient;

 

namespace SelfUpdate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
InitDataGridView();

}

private void button3_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
return;
string strFilePath = FindFilePath();
if (strFilePath != "")
{
string strFileName = strFilePath.Substring(strFilePath.LastIndexOf("\\") + 1);
byte[] byteFileData = File.ReadAllBytes(strFilePath);
string strSql = string.Format("insert into self_file (vName, vPath, vTime, vFile) values ('{0}','{1}','{2}',@file)", strFileName,textBox1.Text,DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
LpSqlServer.InstFile(strSql, byteFileData);
InitDataGridView();
}

}

private void InitDataGridView()
{
string strSql = "select vId, vName, vPath,vTime from self_file";
DataTable dt = LpSqlServer.ReadSql(strSql);
dataGridView1.DataSource = dt;
}

private string FindFilePath()
{
string strResult = "";
OpenFileDialog openFile = new OpenFileDialog();
if (openFile.ShowDialog() == DialogResult.OK)
{
strResult = openFile.FileName;
}
return strResult;

}

private void button2_Click(object sender, EventArgs e)
{
if (dataGridView1.RowCount >0)
{
if (dataGridView1.CurrentCell.RowIndex > -1)
{
string strId = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
string strSql = string.Format("delete from self_file where vId = '{0}'", strId);
LpSqlServer.FlushSql(strSql);
InitDataGridView();
}
}
}

private void button3_Click_1(object sender, EventArgs e)
{
string strSql = "select vName, vPath,vTime,vFile from self_file";
DataTable dt = LpSqlServer.ReadSql(strSql);
for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < dt.Tables[0].Rows[i].ItemArray.Length; j++)
{
string strTemp = dt.Tables[0].Rows[i].ItemArray[j].ToString();
}
}
}
}
}

 

using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Data;
using System.Runtime.InteropServices;
using System.Net;
using System.Data.OleDb;
using System.Collections.Specialized;

namespace LpSpace
{
public class LpFunc
{
//获取调用类库的主程序的运行路径
public static string GetMainPath
{
get
{
//"D:\\SelfFiles\\c#范例\\FirstLibProject\\FirstLibProject\\bin\\Debug\\"
return AppDomain.CurrentDomain.BaseDirectory;
}
}

public static string GetDllFilePath
{
get
{
return System.Reflection.Assembly.GetExecutingAssembly().Location.ToString();
}
}

public static string GetAssemblyPath()
{
string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
_CodeBase = _CodeBase.Substring(8, _CodeBase.Length - 8); // 8是 file:// 的长度
string[] arrSection = _CodeBase.Split(new char[] { '/' });

string _FolderPath = "";
for (int i = 0; i < arrSection.Length - 1; i++)
{
_FolderPath += arrSection[i] + "/";
}
return _FolderPath;
}

//返回主机名和ip地址
public static string[] GetLocalIPAddress()
{
try
{
string[] strArr = new string[2];
//获取登陆当前系统的域账号
//string strTemp = Environment.UserName;
strArr[0] = Dns.GetHostName(); //得到本机的主机名
//IPHostEntry ipEntry = Dns.GetHostByName(strArr[0]); //取得本机IP
IPHostEntry ipEntry = Dns.GetHostEntry(strArr[0]); //取得本机IP
strArr[1] = ipEntry.AddressList[0].ToString();
return strArr;
}
catch
{
return null;
}
}

// Base64加密
public static String SetToBase64(String text)
{
try
{
Byte[] buffer = System.Text.Encoding.Default.GetBytes(text);
String result = Convert.ToBase64String(buffer);
return result;
}
catch
{
return text;
}
}


// Base64解码
public static String GetFromBase64(String text)
{
try
{
Byte[] bufout = Convert.FromBase64String(text);
String result = System.Text.Encoding.Default.GetString(bufout);
return result;
}
catch
{
return text;
}
}
}

/// <summary>
/// Access操作类
/// </summary>
public class LpAccess
{
//public static string strConn = "Provider=Microsoft.jet.OLEDB.4.0;Data source=" + Application.StartupPath + "\\help.mdb";
//可以执行所有刷新数据库状态的语句,返回影响的行数
public static int FlushSql(string strSql)
{
int iResult = -1;
OleDbConnection oleDbConnection = null;
OleDbCommand cmd = null;
try
{
oleDbConnection = new OleDbConnection("strConn");
oleDbConnection.Open();
cmd = new OleDbCommand(strSql, oleDbConnection);
iResult = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{ }
finally
{
oleDbConnection.Close();
oleDbConnection.Dispose();
cmd.Dispose();
}
return iResult;
}

//可以执行所有读取数据库的语句
public static DataTable ReadSql(string strSql)
{
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
OleDbConnection oleDbConnection = null;
OleDbCommand cmd = null;
try
{
oleDbConnection = new OleDbConnection("UCommFunc.g_strSqlConn");
oleDbConnection.Open();
cmd = new OleDbCommand(strSql, oleDbConnection);
da.SelectCommand = cmd;
da.Fill(dt);
}
catch (Exception ex)
{
}
finally
{
oleDbConnection.Close();
oleDbConnection.Dispose();
cmd.Dispose();
}
return dt;
}
}

//"insert into [Lp_Test] (vId,vName,vAge) Values ('1','雷盼','20')";
//"update [Lp_Test] set vAge = '25' where vName = '雷盼'";
//"select * from [Lp_Test] where vId = '1'";
//"delete from [Lp_Test] where vId = '1'";

/// <summary>
/// SqlServer操作类
/// </summary>
public class LpSqlServer
{
public static string strServerIp = "10.144.116.40";
//可以执行所有刷新数据库状态的语句,返回影响的行数
public static int FlushSql(string strSql)
{
int iResult = -1;
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=MyDb");
conn.Open();
cmd = new SqlCommand(strSql, conn);
iResult = cmd.ExecuteNonQuery();
}
catch
{ }
finally
{
conn.Close();
conn.Dispose();
cmd.Dispose();
}
return iResult;
}

//可以执行所有读取数据库的语句
public static DataTable ReadSql(string strSql)
{
SqlDataAdapter sda = new SqlDataAdapter();
DataTable dt = new DataTable();
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
string strCon = "server=.;uid=sa;pwd=123456;database=MyDb";
conn = new SqlConnection(strCon);
conn.Open();
cmd = new SqlCommand(strSql, conn);
sda.SelectCommand = cmd;
sda.Fill(dt);
}
catch
{ }
finally
{
conn.Close();
conn.Dispose();
cmd.Dispose();
}
return dt;
}


//insert into self_file (vFile) values (@file)
public static void InstFile(string strSql,byte[] byteFile)
{
SqlConnection conn = null;
SqlCommand cmd = null;
try
{
string strCon = "server=.;uid=sa;pwd=123456;database=MyDb";
conn = new SqlConnection(strCon);
conn.Open();
cmd = new SqlCommand(strSql, conn);
cmd.Parameters.Add("@file", SqlDbType.Image).Value = byteFile;
cmd.ExecuteNonQuery();
}
catch
{ }
finally
{
conn.Close();
conn.Dispose();
cmd.Dispose();
}
}
}
}

posted @ 2012-07-16 01:33  雷盼  阅读(135)  评论(0编辑  收藏  举报