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;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
Random rnd = new Random(Guid.NewGuid().GetHashCode());
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 這行程式碼會將資料載入 'db1DataSet.asd' 資料表。您可以視需要進行移動或移除。
this.asdTableAdapter.Fill(this.db1DataSet.asd);
textBox1.DataBindings.Add("Text", asdBindingSource, "result");
textBox2.DataBindings.Add("Text", asdBindingSource, "time");
}
private void button1_Click(object sender, EventArgs e)
{
//剪刀
act(0);
}
private void button2_Click(object sender, EventArgs e)
{
//石頭
act(1);
}
private void button3_Click(object sender, EventArgs e)
{
//布
act(2);
}
string tra(int n)
{
if (n == 0)
return "剪刀";
else if (n == 1)
return "石頭";
else if (n == 2)
return "布";
return "GG";
}
void act(int player)
{
//0=剪刀 1=石頭
int computer = rnd.Next(3);
if (player == computer)
{
//平手
label1.Text = "電腦出 " + tra(computer);
textBox1.Text = "平手";
//asdBindingSource.Add(1);
//db1DataSet.asd.
/*db1DataSet.asdDataTable dt = new db1DataSet.asdDataTable();
//DataTable dt = new DataTable();
DataRow dr;
//dt.Columns.Add(new DataColumn("bankname", typeof(string))); //新增一欄位, 欄位名稱為"bankname"
//加入第一筆銀行資料
dr = dt.NewRow();
dr["result"] = "1";
dr["time"] = DateTime.Now;
dt.Rows.Add(dr);
dt.AcceptChanges();
db1DataSet.AcceptChanges();
asdTableAdapter.Update(dt.Rows);
dataGridView1.Update();*/
MessageBox.Show("平手");
}
else if ((player == 0 && computer == 1) ||
(player == 1 && computer == 2) ||
(player == 2 && computer == 0))
{
//電腦贏
label1.Text = "電腦出 " + tra(computer);
textBox1.Text = "輸";
/*textBox1.DataBindings.Clear();
textBox2.DataBindings.Clear();
textBox2.Text = DateTime.Now.ToString();
textBox2.DataBindings.Add("Text", bindingSource1, "time");
textBox1.DataBindings.Add("Text", bindingSource1, "result");*/
MessageBox.Show("電腦贏");
}
else if ((player == 1 && computer == 0) ||
(player == 2 && computer == 1) ||
(player == 0 && computer == 2))
{
//玩家贏
label1.Text = "電腦出 " + tra(computer);
textBox1.Text = "贏";
MessageBox.Show("玩家贏");
}
textBox2.Text = DateTime.Now.ToString();
asdBindingSource.Insert(1,dr);
asdBindingSource.AddNew();
}
/*
void dbUpdate(){
DataSet dsChanges = (DataSet)db1DataSet.GetChanges();
if (dsChanges == null)
{
return;
}
DataTable dtb = new DataTable();
dtb = dsChanges.Tables["products"];
//GetErrors方法返回一個數組,它由表中具一個或多個檢驗錯誤的行構成。如沒有誤返加空數組
DataRow[] badRows = dtb.GetErrors();
if (badRows.Length == 0)
{
//int numRows = suppliersTableAdapter.Update(dsChanges, "products");
//要在需要时生成所需的命令,必须创建 SqlCommandBuilder 对象的实例并使用该构造函数中的 DataAdapter。
//1、
SqlCommandBuilder sqlbuiler = new SqlCommandBuilder();
sqlbuiler.DataAdapter = asdTableAdapter;
//2、
SqlConnection dataConnection = new SqlConnection();
dataConnection.ConnectionString = "Data Source=CCM02\\SQLEXPRESS;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sh2_123";
//dataConnection.Open();
string sCmdText = "select * from products";
//3、
SqlCommand sqlComad = new SqlCommand();
//指出CommandText讀取方式
sqlComad.CommandType = CommandType.Text;
//Connection:連接資料庫的Connection
sqlComad.Connection = dataConnection;
//CommandText:取得或設定要針對資料來源執行的文字命令。
sqlComad.CommandText = sCmdText;
//4、
//suppliersTableAdapter.SelectCommand = new SqlCommand("select * from products ", dataConnection);
//SelectCommand 取得或設定用來在資料來源中選取資料錄的命令。
suppliersTableAdapter.SelectCommand = sqlComad;
//取得或設定接資料庫的Connection
//取得或設定用來將新的資料錄插入至資料來源的命令。
//GetInsertCommand:取得在資料來源上執行插入時所需之自動產生的 DbCommand 物件。
suppliersTableAdapter.InsertCommand = sqlbuiler.GetInsertCommand();
suppliersTableAdapter.UpdateCommand = sqlbuiler.GetUpdateCommand();
suppliersTableAdapter.DeleteCommand = sqlbuiler.GetDeleteCommand();
//5、
int numRows = suppliersTableAdapter.Update(dsChanges, "products");
if (numRows > 0)
{
MessageBox.Show("Update" + numRows + "rows", "Success");
//變更資料
dtSet.AcceptChanges();
}
else
{
//返原所有更
dtSet.RejectChanges();
}
//清除與這個 DbCommandBuilder 關聯的命令。
sqlbuiler.RefreshSchema();
//關閉聯接
dataConnection.Close();
//釋放資源
sqlComad.Dispose();
sqlbuiler.Dispose();
}
else
{
string errorMsg = null;
foreach (DataRow row in badRows)
{
//每一行都可能一個或多個誤,而GetColumnsInError方法將返加一個集合,其中包含了數據有問題的所有例
foreach (DataColumn col in row.GetColumnsInError())
{
//GetColumnError方法獲取一個無效的列的錯誤消息。每一條錯誤消息都附加到errorMsg字符串上。
errorMsg += row.GetColumnError(col) + "\n";
}
}
MessageBox.Show("Errors in data:" + errorMsg, "please fix", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
*/
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}