Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;
Using System.Data.SqlClient;
Using System.IO;
Namespace Winformdemo
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
Let TextBox2 hide
This.textBox2.Visible = false;
Hide the last row of empty values in the DataGridView1 table
This.dataGridView1.AllowUserToAddRows = false;
}
SqlConnection con = new SqlConnection ();
SqlCommand com = new SqlCommand ();
OpenFileDialog open = new OpenFileDialog ();
<summary>
Yes
</summary>
String clickrow = "";
<summary>
Column
</summary>
String clickcells = "";
<summary>
A string that adds rows and columns
</summary>
String sqllanding = "server=.; Uid=sa;pwd=123456789;database=myfirstdemo ";
private void Datagridview1_cellcontentclick (object sender, DataGridViewCellEventArgs e)
{
Gets the row and column that is being clicked.
Clickrow = This.datagridview1.rows[e.rowindex]. Cells[0]. Value.tostring ();
Clickcells = This.datagridview1.rows[e.rowindex]. CELLS[1]. Value.tostring ();
}
private void Form1_Load (object sender, EventArgs e)
{
Selectinfo ();
}
public void Selectinfo ()
{
Broken Links View Database data
Con. ConnectionString = sqllanding;
Com.commandtext = "SELECT name as filename, txtlujing as file path from Txtbianjiqi";
Com. Connection = con;
DataSet ds = new DataSet ();
SqlDataAdapter SDA = new SqlDataAdapter (COM);
Sda. Fill (DS);
This.dataGridView1.DataSource = ds. Tables[0];
}
private void Open Toolstripmenuitem_click (object sender, EventArgs e)
{
String Filepath = Clickcells + clickrow;
This.textBox2.Visible = true;
Try
{
Read-only stream;
FileStream FSS = new FileStream (Filepath, FileMode.OpenOrCreate, FileAccess.Read);
StreamReader SWW = new StreamReader (FSS, Encoding.default);
TextBox2.Text = SWW. ReadToEnd ();
Sww. Close ();
Fss. Close ();
}
catch (Exception ex)
{
If no path is selected to prompt a sentence;
MessageBox.Show ("View path Error:" + ex.) Message);
}
}
private void Save Toolstripmenuitem_click (object sender, EventArgs e)
{
String Filepath = Clickcells + clickrow;
Try
{
Write-only stream;
FileStream FSS = new FileStream (Filepath, FileMode.Create, FileAccess.Write);
StreamWriter SWW = new StreamWriter (FSS, Encoding.default);
Sww. Write (TextBox2.Text);
Sww. Close ();
Fss. Close ();
MessageBox.Show ("Saved successfully! ");
}
catch (Exception ex)
{
If no path is selected to prompt a sentence;
MessageBox.Show ("Save path Error:" + ex.) Message);
}
This.textBox2.Visible = false;
}
private void new Toolstripmenuitem_click (object sender, EventArgs e)
{
This.textBox2.Text = "";
String localfilepath = "";
String filenameext = "";
String flie = "";
SaveFileDialog SaveFileDialog = new SaveFileDialog ();
Open the default file directory
Savefiledialog.initialdirectory = "d:\\\\text\\";
File suffix name
Savefiledialog.filter = "text file (*.txt) |*.txt| All Files (*. *) |*.*";
Savefiledialog.filterindex = 2;
string lujing = Savefiledialog.initialdirectory;
if (savefiledialog.showdialog () = = DialogResult.OK)
{
Flie = Savefiledialog.filename;
File directory Name
Localfilepath = SaveFileDialog.FileName.ToString ();
Intercept file name
Filenameext = localfilepath.substring (localfilepath.lastindexof ("\ \") + 1);
}
String sql = "SELECT name from Txtbianjiqi";
SqlCommand CO = new SqlCommand (sql, con);
SqlDataAdapter da = new SqlDataAdapter (CO);
DataSet DSS = new DataSet ();
Da. Fill (DSS);
Loop to determine the name in the incoming table
for (int i = 0; i < DSS. Tables[0]. Rows.Count; i++)
{
Set a variable to get it out of the name
string SS = DSS. Tables[0]. Rows[i][0]. ToString ();
Determines whether the value entered in the dialog box is the same as the name found
if (Filenameext = = ss)
{
MessageBox.Show ("The file has changed! ");
Return
}
}
Try
{
Write-only stream
FileStream fs = new FileStream (Flie, FileMode.Create, FileAccess.Write);
StreamWriter SW = new StreamWriter (FS, encoding.default);//dialog box Save As.
Sw. Write (TextBox2.Text);
Sw. Flush ();
Fs. Close ();
Con. ConnectionString = sqllanding;
Add a file name and path name SQL statement to the database
Com.commandtext = String.Format ("INSERT into Txtbianjiqi (name,txtlujing) VALUES (' {0} ', ' {1} ')", Filenameext, lujing);
Com. Connection = con;
Con. Open ();
int insertinto = Convert.ToInt32 (com. ExecuteScalar ());
if (Insertinto > 0)
{
MessageBox.Show ("The operation failed! Please try again. ");
}
Else
{
MessageBox.Show ("added successfully! ");
This.textBox2.Visible = false;
}
}
catch (Exception ex)
{
MessageBox.Show ("Add Log failed:" + ex.) Message);
}
Con. Close ();
Selectinfo ();
}
private void Delete Toolstripmenuitem_click (object sender, EventArgs e)
{
Con. ConnectionString = sqllanding;
Delete the file name you are tapping from the database
Com.commandtext = String.Format ("Delete from Txtbianjiqi where Name= ' {0} '", Clickrow);
Com. Connection = con;
Con. Open ();
DialogResult dr = MessageBox.Show ("Confirm deletion?") "," hint ", Messageboxbuttons.okcancel, MessageBoxIcon.Information);
if (dr = = DialogResult.OK)
{
int insertinto = Convert.ToInt32 (com. ExecuteScalar ());
if (Insertinto > 0)
{
MessageBox.Show ("Operation Error!! ");
}
Else
{
File.delete (Clickcells + clickrow); Delete the files in Windows, in parentheses, the path to delete the document.
File.delete (Clickcells + clickrow);
MessageBox.Show ("Delete succeeded! ");
}
}
Con. Close ();
Selectinfo ();
}
private void Exit Toolstripmenuitem_click (object sender, EventArgs e)
{
This. Close ();
}
}
}
is to write a very simple in the WinForm to fill in the text, the use of ADO to link the database, in the new text need to write the. txt suffix, open or delete the need to first click on the text name. Please forgive me for your lack of writing!
Simple WinForm Editor