 |
|
ทำบน Sharepoint ค่ะ เป็นการเขียน Infopath เชื่อมกับ Visual Studio ใช้ DB ของ ms sql server
โค้ดหน้าที่ใช้จริงๆ
Code (C#)
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace RequestForm
{
public partial class FormCode
{
DBConnect DB = new DBConnect();
// Member variables are not supported in browser-enabled forms.
// Instead, write and read these values from the FormState
// dictionary using code such as the following:
//
// private object _memberVariable
// {
// get
// {
// return FormState["_memberVariable"];
// }
// set
// {
// FormState["_memberVariable"] = value;
// }
// }
// NOTE: The following procedure is required by Microsoft InfoPath.
// It can be modified using Microsoft InfoPath.
public void InternalStartup()
{
EventManager.XmlEvents["/my:myFields/my:abFromDate"].Changed += new XmlChangedEventHandler(abFromDate_Changed);
EventManager.XmlEvents["/my:myFields/my:abEndDate"].Changed += new XmlChangedEventHandler(abEndDate_Changed);
}
public void abFromDate_Changed(object sender, XmlEventArgs e)
{
// Write your code here to change the main data source.
XPathNavigator nav = MainDataSource.CreateNavigator();
string aFromDate = nav.SelectSingleNode("//my:abFromDate", NamespaceManager).Value;
string aEndDate = nav.SelectSingleNode("//my:abEndDate", NamespaceManager).Value;
String strSQL;
DataSet ds;
strSQL = "SELECT Count(*) HDate FROM Holidays where HolidaysDate between '" + aFromDate + "' AND '" + aEndDate + "'";
ds = DB.QueryDataSet(strSQL);
string x = ds.Tables[0].Rows[0][0].ToString();
DB.Close();
//-----------------
strSQL = "select datediff(day,'" + aFromDate + "','" + aEndDate + "') as DateDD";
ds = DB.QueryDataSet(strSQL);
string y = ds.Tables[0].Rows[0][0].ToString();
DB.Close();
int xx = Convert.ToInt32(y) - Convert.ToInt32(x);
nav.SelectSingleNode("//my:abTotalDate", NamespaceManager).SetValue(xx.ToString());
}
public void abEndDate_Changed(object sender, XmlEventArgs e)
{
// Write your code here to change the main data source.
XPathNavigator nav = MainDataSource.CreateNavigator();
string aFromDate = nav.SelectSingleNode("//myFields:abFromDate", NamespaceManager).Value;
string aEndDate = nav.SelectSingleNode("//myFields:abEndDate", NamespaceManager).Value;
String strSQL;
DataSet ds;
strSQL = "SELECT Count(*) HDate FROM Holidays where HolidaysDate between '" + aFromDate + "' AND '" + aEndDate + "'";
ds = DB.QueryDataSet(strSQL);
string x = ds.Tables[0].Rows[0][0].ToString();
DB.Close();
//-----------------
strSQL = "select datediff(day,'" + aFromDate + "','" + aEndDate + "') as DateDD";
ds = DB.QueryDataSet(strSQL);
string y = ds.Tables[0].Rows[0][0].ToString();
DB.Close();
int xx = Convert.ToInt32(y) - Convert.ToInt32(x);
nav.SelectSingleNode("//myFields:abTotalDate", NamespaceManager).SetValue(xx.ToString());
}
}
}
โค้ด DB connect
Code (C#)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace RequestForm
{
class DBConnect
{
private SqlConnection objConn;
private SqlCommand objCmd;
private SqlTransaction Trans;
private String strConnString;
private String strCommandString;
public DBConnect()
{
strConnString = "Server=192.168.1.2;Database=HR;Trusted_Connection=True;";
}
public SqlDataReader QueryDataReader(String strSQL)
{
SqlDataReader dtReader;
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new SqlCommand(strSQL, objConn);
dtReader = objCmd.ExecuteReader();
return dtReader; //*** Return DataReader ***//
}
public DataSet QueryDataSet(String strSQL)
{
DataSet ds = new DataSet();
SqlDataAdapter dtAdapter = new SqlDataAdapter();
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
objCmd = new SqlCommand();
objCmd.Connection = objConn;
objCmd.CommandText = strSQL;
objCmd.CommandType = CommandType.Text;
dtAdapter.SelectCommand = objCmd;
dtAdapter.Fill(ds);
return ds; //*** Return DataSet ***//
}
public DataTable QueryDataTable(String strSQL)
{
SqlDataAdapter dtAdapter;
DataTable dt = new DataTable();
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
dtAdapter = new SqlDataAdapter(strSQL, objConn);
dtAdapter.Fill(dt);
return dt; //*** Return DataTable ***//
}
public Boolean QueryExecuteNonQuery(String strSQL)
{
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
try
{
objCmd = new SqlCommand();
objCmd.Connection = objConn;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
return true; //*** Return True ***//
}
catch (Exception)
{
return false; //*** Return False ***//
}
}
public Object QueryExecuteScalar(String strSQL)
{
Object obj;
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
try
{
objCmd = new SqlCommand();
objCmd.Connection = objConn;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
obj = objCmd.ExecuteScalar(); //*** Return Scalar ***//
return obj;
}
catch (Exception)
{
return null; //*** Return Nothing ***//
}
}
public void TransStart()
{
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objConn.Open();
Trans = objConn.BeginTransaction(IsolationLevel.ReadCommitted);
}
public void TransExecute(String strSQL)
{
objCmd = new SqlCommand();
objCmd.Connection = objConn;
objCmd.Transaction = Trans;
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strSQL;
objCmd.ExecuteNonQuery();
}
public void TransRollBack()
{
Trans.Rollback();
}
public void TransCommit()
{
Trans.Commit();
}
public void Close()
{
objConn.Close();
objConn = null;
}
public string CommandString
{
get
{
return strCommandString;
}
set
{
strCommandString = value;
objConn = new SqlConnection();
objConn.ConnectionString = strConnString;
objCmd = new SqlCommand(strCommandString, objConn);
}
}
public void AddParameter(string ParameterName, object ParameterValue)
{
objCmd.Parameters.AddWithValue(ParameterName, ParameterValue);
}
public bool ExecuteNonQuery()
{
int rowsAffected = 0;
try
{
objConn.Open();
rowsAffected = objCmd.ExecuteNonQuery();
objConn.Close();
}
catch (Exception)
{
rowsAffected = 0;
}
return (rowsAffected > 0) ? true : false;
}
}
}
ผลการรัน คือ Build ผ่าน แต่พอกด Control เพื่อเปลี่ยนวันที่ปุ๊บ ขึ้นError ที่บรรทัด objConn.Open(); ว่า
Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
ก่อนหน้านี้ ก่อนที่จะเอาโค้ดนี้มาใส่ Sharepoint ลองทำเป็น ASP.NET โค้ดเหมือนกันทุกอย่างเลยค่ะ รันผ่าน คำนวณให้ ไม่มีปัญหาเลย
รบกวนช่วยดูให้หน่อยค่ะ ว่ามันเป็นอะไร งงไปหมดแล้ว
Tag : .NET, Ms SQL Server 2008, C#, VS 2010 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2010-12-21 12:16:53 |
By :
Mink |
View :
2067 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |