using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Configuration;
namespace WinTools
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
//方便工作管理
//完成工作好找资料 可以很更快捷 把快捷方式和文件复制拖动到上面。
this.timer1.Enabled = true;
}
private void listBox2_DragDrop(object sender, DragEventArgs e)
{
string fname = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
foreach (string str in listBox2.Items)
{
if (fname == str)
{
return;
}
}
listBox2.Items.Add(fname);
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
string fname = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
foreach (string str in listBox1.Items)
{
if (fname == str)
{
return;
}
}
listBox1.Items.Add(fname);
}
private void listBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(listBox2.SelectedItem.ToString());
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(this.tn.Text))
{
object ID = Extits();
if (ID == null)
{
this.textBoxID.Text = Save();
}
else
{
this.textBoxID.Text = ID.ToString();
UpdateData();
}
label6.Text = "保存成功!";
}
}
private string Save()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["MyDBConString"].ToString()))
{
try
{
conn.Open();
string cmdText = @"INSERT INTO [dbo].[Work]
([Name]
,[StartTime]
,[EndTime]
,[State]
,[JoinUser]
,[FileQuick]
,[LinkQuick]
)
VALUES
(@Name
,@StartTime
,@EndTime
,@State
,@JoinUser
,@FileQuick
,@LinkQuick
);select SCOPE_IDENTITY() as id";
//如果是sql server 最好用select SCOPE_IDENTITY() as id
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.Parameters.AddWithValue("@Name", this.tn.Text.Trim());
cmd.Parameters.AddWithValue("@StartTime", this.ts.Value);
cmd.Parameters.AddWithValue("@EndTime", this.te.Value);
cmd.Parameters.AddWithValue("@State", this.ts.Text.Trim());
cmd.Parameters.AddWithValue("@JoinUser", this.tj.Text.Trim());
cmd.Parameters.AddWithValue("@FileQuick", GetString1());
cmd.Parameters.AddWithValue("@LinkQuick", GetString2());
return Convert.ToString(cmd.ExecuteScalar());
}
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
return "0";
}
private void UpdateData()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["MyDBConString"].ToString()))
{
try
{
conn.Open();
string cmdText = @"UPDATE [dbo].[Work]
SET [Name] = @Name
,[StartTime] = @StartTime
,[EndTime] = @EndTime
,[State] = @State
,[JoinUser] = @JoinUser
,[FileQuick] = @FileQuick
,[LinkQuick] = @LinkQuick
WHERE [ID]=@ID";
//如果是sql server 最好用select SCOPE_IDENTITY() as id
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.Parameters.AddWithValue("@Name", this.tn.Text.Trim());
cmd.Parameters.AddWithValue("@StartTime", this.ts.Value);
cmd.Parameters.AddWithValue("@EndTime", this.te.Value);
cmd.Parameters.AddWithValue("@State", this.ts.Text.Trim());
cmd.Parameters.AddWithValue("@JoinUser", this.tj.Text.Trim());
cmd.Parameters.AddWithValue("@FileQuick", GetString1());
cmd.Parameters.AddWithValue("@LinkQuick", GetString2());
cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(this.textBoxID.Text.Trim()));
cmd.ExecuteNonQuery();
}
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
}
private string GetString1()
{
string vvv = string.Empty;
foreach (string item in this.listBox1.Items)
{
vvv += item;
}
return vvv;
}
private string GetString2()
{
string vvv = string.Empty;
foreach (string item in this.listBox2.Items)
{
vvv += item + "\n";
}
return vvv;
}
private object Extits()
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["MyDBConString"].ToString()))
{
try
{
conn.Open();
string cmdText = @"SELECT [ID] FROM [dbo].[Work] WHERE [Name]=@Name";
//如果是sql server 最好用select SCOPE_IDENTITY() as id
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
cmd.Parameters.AddWithValue("@Name", this.tn.Text.Trim());
return cmd.ExecuteScalar();
}
}
finally
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
return null;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(this.textBoxID.Text) && this.textBoxID.Text != "0")
{
if (!string.IsNullOrWhiteSpace(this.tn.Text))
{
object ID = Extits();
if (ID == null)
{
this.textBoxID.Text = Save();
}
else
{
this.textBoxID.Text = ID.ToString();
UpdateData();
}
}
}
}
}
}