using clearParameter() even thought PreparedStatement is being closed - sql

private PreparedStatement InsertPS = null;
public boolean InsertInDB(String username, String password, double balance, String secret) {
boolean ans = false;
try {
InsertPS = con.prepareStatement("Insert into BankDB values(?,?,?,?)");
String data[] = AMC.SendtoDB(password, secret);
InsertPS.setString(1, data[0]);
InsertPS.setString(2, username);
InsertPS.setString(3, data[1]);
InsertPS.setDouble(4, balance);
int rows = InsertPS.executeUpdate();
if (rows != 0) {
ans = true;
}
InsertPS.clearParameters();
} catch (SQLException sqlInite) {
System.out.println("SQL Error in InsertInDB method: " + sqlInite);
} finally {
try {
InsertPS.close();
} catch (SQLException sqle) {
System.out.println("SQL Exception in InsertInDB method finally clause : " + sqle);
}
}
return ans;
}
Above is the InsertInDB() method given,
It has a InsertPS PreparedStatement Object.
Here is it necessary to use clearParameters() method even though i am closing the InsertPS object at the end of the method.
(I have provided a separate method to close the connection object)
also another question: Is it a good idea to create PreparedStatement Object's outside any method within a class,initializing using Constructor and Say for example Once all Object's (each in different method) are used, close all PreparedStatement Objects using a separate method.
public class JavatoDB {
Driver DM = null;
Connection con = null;
PreparedStatement InsertPS = null;
PreparedStatement BalancePS = null;
PreparedStatement DeletePS = null;
PreparedStatement UpdatePS = null;
PreparedStatement SearchDB = null;
ResultSet RS = null;
ResultSetMetaData RSMD = null;
AdminControl AMC = null;
public JavatoDB() {
AMC = new AdminControl();
try {
DM = new com.mysql.jdbc.Driver();
con = DriverManager.getConnection("jdbc:mysql://localhost/javadb", "java", "javaaccess");
InsertPS = con.prepareStatement("Insert into BankDB values(?,?,?,?)");
BalancePS = con.prepareStatement("Select BALANCE from BankDB where ACCNAME=? AND ACCPIN = ?");
DeletePS = con.prepareStatement("Delete from BankDB where ACCNAME = ? AND ACCPIN = ? ");
UpdatePS = con.prepareStatement("Update BankDB set BALANCE = (BALANCE + ?) where ACCNAME = ? AND ACCPIN = ?");
SearchDB = con.prepareStatement("Select ID AND ACCPIN from BankDB where ACCNAME = ? ");
} catch (SQLException JavatoDBContrsuctor) {
System.out.println("SQL Error in JavatoDBConstructor: " + JavatoDBContrsuctor);
}
}
public boolean InsertInDB(String username, String password, double balance, String secret) {
boolean ans = false;
try {
String data[] = AMC.SendtoDB(password, secret);
InsertPS.setString(1, data[0]);
InsertPS.setString(2, username);
InsertPS.setString(3, data[1]);
InsertPS.setDouble(4, balance);
int rows = InsertPS.executeUpdate();
if (rows != 0) {
ans = true;
}
InsertPS.clearParameters();
} catch (SQLException sqlInite) {
System.out.println("SQL Error in InsertInDB method: " + sqlInite);
}
return ans;
}
Suggestion's or Criticism on other aspects of code are also welcome.

For the first question, when you call:
con.prepareStatement()
a new preparedStatement is created, then parameters dont survive, i mean you dont need to clear then.
About the second question, in your implementation i cant see where you close your preparedStatement, and if you only add close, next time method will fail. Then, it is usual to create the preparedStatement and close it in the same method.

Related

Entity Framework in stock market

public List<StockMarket> ReadAllRecords(string TxtFilePath, string TxtFileName)
{
List<StockMarket> Stock = new List<StockMarket>();
String[] a = File.ReadAllLines(TxtFilePath + TxtFileName);
foreach (var b in a)
{
String[] d = b.Split(',');
StockMarket S = new StockMarket();
S.ProductId = d[0];
S.ProductName = d[1];
S.StockId = d[2];
S.StockName = d[3];
S.StockPrice = d[4];
S.NumberofStocks = d[5];
S.Currency = d[6];
Stock.Add(S);
}
return Stock;
}
public List<StockMarket> GetValidRecords(List<StockMarket> Stock, string ErrorFilePath, string ErrorFileName)
{
List<StockMarket> Valid = new List<StockMarket>();
List<StockMarket> InValid = new List<StockMarket>();
foreach (var s in Stock)
{
bool ValidRecord = true;
if (String.IsNullOrEmpty(s.ProductId) || !s.ProductId.All(Char.IsDigit))
{
ValidRecord = false;
}
if (!s.ProductName.StartsWith("ABC") || s.ProductName.Length != 6)
{
ValidRecord = false;
}
if (String.IsNullOrEmpty(s.StockId) || !s.StockId.All(Char.IsDigit))
{
ValidRecord = false;
}
if (!s.StockName.StartsWith("SBC") || s.StockName.Length != 7)
{
ValidRecord = false;
}
if (string.IsNullOrEmpty(s.StockPrice))
{
ValidRecord = false;
}
if (string.IsNullOrEmpty(s.NumberofStocks) || !s.NumberofStocks.All(char.IsDigit))
{
ValidRecord = false;
}
if (!(s.Currency.Equals("INR") || s.Currency.Equals("USD") || s.Currency.Equals("EUR")))
{
ValidRecord = false;
}
if (ValidRecord)
{
Valid.Add(s);
}
else
{
InValid.Add(s);
}
}
LogErrorRecord(InValid, ErrorFilePath, ErrorFileName);
return Valid;
}
public List<StockMarket> CalculateTotalPrice(List<StockMarket> Stock)
{
foreach (var s in Stock)
{
if (s.Currency.Equals("INR"))
{
s.TotalPrice = (Convert.ToDouble(s.StockPrice) * Convert.ToDouble(s.NumberofStocks) * 1).ToString();
}
else if (s.Currency.Equals("USD"))
{
s.TotalPrice = (Convert.ToDouble(s.StockPrice) * Convert.ToDouble(s.NumberofStocks) * 0.5).ToString();
}
else if (s.Currency.Equals("EUR"))
{
s.TotalPrice = (Convert.ToDouble(s.StockPrice) * Convert.ToDouble(s.NumberofStocks) * 0.75).ToString();
}
}
return Stock;
}
public void LogErrorRecord(List<StockMarket> InvalidStock, string ErrorFilePath, string ErrorFileName)
{
List<String> InvalidItems = new List<string>();
foreach (var I in InvalidStock)
{
InvalidItems.Add(I.ProductId + " " + I.ProductName + " " + I.StockId + " " + I.StockName + " " + I.StockPrice + " " + I.NumberofStocks + " " + I.Currency);
}
File.AppendAllLines(ErrorFilePath + ErrorFileName, InvalidItems);
}
public void SavetoDB(List<StockMarket> Stock, SqlConnection connection)
{
String Query = "insert into StockMarket(ProductId,Productname,StockId,StockName,StockPrice,NumberofStocks,Currency,TotalPrice) Values(#ProductId,#ProductName,#StockId,#StockName,#StockPrice,#NumberofStocks,#Currency,#TotalPrice)";
connection.Open();
foreach (var a in Stock)
{
SqlCommand cmd = new SqlCommand(Query, connection);
cmd.Parameters.Add("#ProductId", a.ProductId);
cmd.Parameters.Add("#ProductName", a.ProductName);
cmd.Parameters.Add("#StockId", a.StockId);
cmd.Parameters.Add("#StockName", a.StockName);
cmd.Parameters.Add("#StockPrice", a.StockPrice);
cmd.Parameters.Add("#NumberofStocks", a.NumberofStocks);
cmd.Parameters.Add("#Currency", a.Currency);
cmd.Parameters.Add("#TotalPrice", a.TotalPrice);
int b = cmd.ExecuteNonQuery();
}
connection.Close();
}
public void SaveDistinctProductName(List<StockMarket> Stock, SqlConnection connection)
{
String Query = "if not exists( select * from Product where ProductId = #ProductId) begin insert into Product (ProductId,ProductName) Values(#ProductId,#ProductName)end";
connection.Open();
foreach (var a in Stock)
{
SqlCommand cmd = new SqlCommand(Query, connection);
cmd.Parameters.Add("#ProductId", a.ProductId);
cmd.Parameters.Add("#ProductName", a.ProductName);
int b = cmd.ExecuteNonQuery();
}
connection.Close();
}
public void SaveDistinctStockName(List<StockMarket> Stock, SqlConnection connection)
{
String Query = "if not exists( select * from Stock where StockId = #StockId) begin insert into Stock (StockId,StockName) Values(#StockId,#StockName)end";
connection.Open();
foreach (var a in Stock)
{
SqlCommand cmd = new SqlCommand(Query, connection);
cmd.Parameters.Add("#StockId", a.StockId);
cmd.Parameters.Add("#StockName", a.StockName);
int b = cmd.ExecuteNonQuery();
}
connection.Close();
}
Entity Framework allows you to create a model by writing code or using boxes and lines in the EF Designer. Both of these approaches can be used to target an existing database or create a new database. This short video explains the differences and how to find the one that is right for you.
Please let me know the overview of the code
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
namespace EmployeeValidation
{
public class Program
{
public static void Main()
{
/*
* Pass the file path, file names and connection string if any in this method alone.
* Do not hardcode in any other methods
*/
SqlConnection connection = new SqlConnection(#"Data Source=NA03OSDVP00746\SQLEXPRESS;Initial Catalog=DBEmployeeValidation;Integrated Security=True");
EmployeeValidator empValidator = new EmployeeValidator();
empValidator.ProcessData(#"D:\Employee_Validator\Input File\", "Emp_122014.xml", #"D:\Employee_Validator\Error File\", "Emp_122014.xml", connection);
}
}
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Xml.Linq;
using System.Linq;
using System.Text.RegularExpressions;
using System.Data.Linq;
using System.Globalization;
using System.Xml.Serialization;
using System.IO;
namespace EmployeeValidation
{
public class EmployeeValidator
{
/*
* Do not remove the attached TestProject. It is meant for auto evaluation of your source code.
* Do not attach any test classess to the attached test project.
* Do not attach any new test projects.
* You are not required to write any automated test cases. You are supposed to write only the code.
*/
public void ProcessData(string xmlFilePath, string xmlFileName,string errorFilePath, string errorFileName, SqlConnection connection)
{
//Do your logic here
//Step 1
//ReadAllEmployeesFromXmlFile
List<Employee> lstemp = new List<Employee>();
List<Employee> validemp = new List<Employee>();
lstemp = ReadAllEmployeesFromXmlFile(xmlFilePath,xmlFileName);
validemp = PickValidEmployees(lstemp);
SaveValidEmployeesToDB(validemp, connection);
ReadfromDBtoTxt(connection);
//Step 2
//PickValidEmployees
//Step 3
//SaveValidEmployeesToDataBase
}
public List<Employee> ReadAllEmployeesFromXmlFile(string xmlFilePath, string xmlFileName)
{
//Read the employee details from the xml file and return it in List collection
//Do not hardcode the filename and the file path here
//Do not return the date with time appended to it.
string employeefile = xmlFilePath + xmlFileName;
List<Employee> empdetail = new List<Employee>();
XElement getelementfile = XElement.Load(employeefile);
IEnumerable<XElement> items = getelementfile.Elements();
foreach (var item in items)
{
string _EmployeeId = item.Element("EmployeeId").Value;
string _EmployeeName = item.Element("EmployeeName").Value;
string _EmailId = item.Element("EmailId").Value;
string _DateOfJoining = item.Element("DateOfJoining").Value;
empdetail.Add(new Employee(){ EmployeeId= _EmployeeId,
EmployeeName= _EmployeeName,
EmailId=_EmailId,
DateOfJoining=_DateOfJoining
});
}
return empdetail;
}
public List<Employee> PickValidEmployees(List<Employee> employees)
{
//Pick the valid employees from the List collection
//Return the valid employees in a List
List<Employee> valid = new List<Employee>();
List<Employee> Invalid = new List<Employee>();
List<string> empnum = new List<string>();
bool isvalid = true;
foreach(Employee em in employees)
{
Regex rgxisnumeric = new Regex(#"^\d$");
Regex rgxisalphanumeric=new Regex( #"^\d*[a-zA-Z]{1,}\d*");
Regex rgxemail = new Regex(#"^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$");
Regex rgxDate= new Regex(#"^((0[1-9]|1[0-2])\/((0|1)[0-9]|2[0-9]|3[0-1])\/((19|20)\d\d))$");
if (!empnum.Contains(em.EmployeeId))
{
empnum.Add(em.EmployeeId);
isvalid = true;
}
else
{
isvalid = false;
}
int empname;
isvalid= isvalid && (!string.IsNullOrEmpty(em.EmployeeId)) && (rgxisnumeric.IsMatch(em.EmployeeId));
isvalid= isvalid && (int.TryParse(em.EmployeeName, out empname)== false);
isvalid= isvalid && (!string.IsNullOrEmpty(em.EmployeeName)) && (rgxisalphanumeric.IsMatch(em.EmployeeName));
isvalid= isvalid && (!string.IsNullOrEmpty(em.EmailId)) && (rgxemail.IsMatch(em.EmailId));
isvalid= isvalid && (!string.IsNullOrEmpty(em.DateOfJoining)) && (rgxDate.IsMatch(em.DateOfJoining));
if(isvalid)
{
DateTime dt;
isvalid= isvalid && DateTime.TryParseExact(em.DateOfJoining,"MM/dd/yyyy",new CultureInfo("en-US"),DateTimeStyles.None, out dt);
}
if(isvalid)
{
valid.Add(em);
}
else
{
Invalid.Add(em);
}
}
SaveInValidEmployeesTotxt(Invalid);
return valid;//Return only valid employees in List
}
public void SaveValidEmployeesToDB(List<Employee> employees, SqlConnection connection)
{
//Do not Prefix Database name in the SQL Query. Query should be "Insert into SBA.TableName"
//Should not be "Insert into DatabaseName.SBA.TableName"
//Do not hardcode the connection string here
SqlConnection conn = connection;
foreach(Employee emp in employees)
{
string command = "Insert into SBA.Employees (EmployeeId,EmployeeName,EmailId,DateOfJoining) values (#EmployeeId,#EmployeeName,#EmailId,#DateOfJoining)";
SqlCommand cmd = new SqlCommand(command, conn);
conn.Open();
cmd.Parameters.AddWithValue("#EmployeeId",emp.EmployeeId);
cmd.Parameters.AddWithValue("#EmployeeName",emp.EmployeeName);
cmd.Parameters.AddWithValue("#EmailId",emp.EmailId);
cmd.Parameters.AddWithValue("#DateOfJoining",DateTime.Parse(emp.DateOfJoining).ToString("MM/dd/yyyy"));
cmd.ExecuteNonQuery();
conn.Close();
}
}
public void SaveInValidEmployeesTotxt(List<Employee> Invalid)
{
string invalidpath = #"D:\Employee_Validator\Error File\Emp_122014.xml";
XmlSerializer serialise = new XmlSerializer(typeof(List<Employee>));
TextWriter writeinvalid = new StreamWriter(invalidpath);
serialise.Serialize(writeinvalid,Invalid);
}
public void ReadfromDBtoTxt(SqlConnection connection)
{
string newfilepath = #"D:\Employee_Validator\DBtoTXT\EmpoValid_" + DateTime.Now.ToString("MMyyyy") + ".txt";
List<Employee> dbtotextlist = new List<Employee>();
if (!File.Exists(newfilepath))
{
var g= File.Create(newfilepath);
g.Close();
}
SqlCommand cmd = new SqlCommand("Select * from SBA.Employees",connection);
connection.Open();
SqlDataReader readdata = cmd.ExecuteReader();
while (readdata.Read())
{
dbtotextlist.Add(new Employee
{
EmployeeId = readdata["EmployeeId"].ToString(),
EmployeeName = readdata["EmployeeName"].ToString(),
EmailId = readdata["EmailId"].ToString(),
DateOfJoining = readdata["DateOfJoining"].ToString()
});
}
sconnection.Close();
StreamWriter sw = File.AppendText(newfilepath);
foreach(Employee s in dbtotextlist)
{
sw.WriteLine(s.EmployeeId+","+s.EmployeeName+","+s.EmailId+","+ DateTime.Parse(s.DateOfJoining).ToString("MM/dd/yyyy"));
}
sw.Flush();
sw.Close();
}
}
}
static void Main(string[] args)
{
SqlConnection connectionObject = new SqlConnection(#"Data Source=NA03OSDVP00746\SQLEXPRESS;Initial Catalog= DBFXCalculation;Integrated Security=True");
Main fxcalculatorobj = new Main();
fxcalculatorobj.ProcessData(#"D:\frameworksample\Input File\", "TradeOrders_032013.txt",
#"D:\frameworksample\ErrorLog\", "InvalidRecords_032014.txt", connectionObject,
#"D:\frameworksample\Archive\", "TradeOrders_032013_Processed.txt");
/*
* Pass the file path, file names and connection string in this method alone.
* Do not hardcode in any other methods
*/
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Reflection;
namespace frameworksample
{
class Main
{
public void ProcessData(string sourceFolder, string fileName, string errorLogFilePath,
string errorLogFileName, SqlConnection connectionObject,
string archiveFilePath, string archiveFileName)
{
//Step 1:ReadAllDataFromInputFile
List<Trade> trades = ReadAllDataFromInputFile(sourceFolder, fileName);
//Step 2:PickValidTradeDetails;
List<Trade> validateTrade = PickValidTradeDetails(trades, errorLogFilePath, errorLogFileName);
//Step 3: SaveValidRecordsToDB
SaveValidRecordsToDB(validateTrade, connectionObject);
//Step 4:CalculateFXRate
List<FXRate> fxRates = CalculateFXRate(connectionObject);
//Step 5:SaveFXRate
//List<FXRate> fxRates = new List<FXRate>();
SaveFXRate(fxRates, connectionObject);
//Step 6:CopyToArchive
CopyToArchive(archiveFilePath, archiveFileName);
//Validate data
}
public List<Trade> ReadAllDataFromInputFile(string sourceFolder, string fileName)
{
List<Trade> trades = new List<Trade>();
//Do your logic to read file and storing it into list of trades ..here..
//Do not hardcode the filename and the file path here
using(StreamReader sr= File.OpenText(sourceFolder+fileName))
{
string s="";
while((s=sr.ReadLine())!=null)
{
if(s.Contains(','))
{
string[] splited= s.Split(',');
Trade tradeitem= new Trade();
tradeitem.TradeId= splited[0];
tradeitem.ISIN= splited[1];
tradeitem.TradeDate= splited[2];
tradeitem.MaturityDate= splited[3];
tradeitem.SchemeName= splited[4];
tradeitem.TradeType= splited[5];
tradeitem.Currency= splited[6];
tradeitem.Amount= splited[7];
trades.Add(tradeitem);
}
}
}
return trades;
}
public List<Trade> PickValidTradeDetails(List<Trade> trades, string errorLogFilePath, string errorLogFileName)
{
//email: \w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)* ,,,, \b[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,4}\b
// Step 1 : filter the valid trades and invalid trades, save the invalid
List<Trade> validTrades = new List<Trade>(); //identify all the valid trades and assign.
//Do not hardcode the filename and the file path here
List<Trade> invalidTrade = new List<Trade>();
bool isValid = true;
foreach (Trade x in trades)
{
isValid=(!string.IsNullOrEmpty(x.TradeId));
Regex rgxtradeid = new Regex(#"\bTR\d{3}\b");
isValid = isValid && rgxtradeid.IsMatch(x.TradeId);
isValid=(isValid && (!string.IsNullOrEmpty(x.ISIN)));
Regex rgxisin = new Regex(#"\bISIN\d{3}\b");
isValid = (rgxisin.IsMatch(x.ISIN));
Regex rgxdate = new Regex("((0[1-9]|1[0-2])\\/((0|1)[0-9]|2[0-9]|3[0-1])\\/((19|20)\\d\\d))$");
DateTime dt;
isValid = (isValid && (!string.IsNullOrEmpty(x.TradeDate)) && (rgxdate.IsMatch(x.TradeDate)));
isValid = (isValid && (!string.IsNullOrEmpty(x.MaturityDate)) && (rgxdate.IsMatch(x.MaturityDate)));
if (isValid)
{
DateTime MD = DateTime.Parse(x.MaturityDate);
DateTime TD = DateTime.Parse(x.TradeDate);
int year = MD.Year - TD.Year;
isValid = isValid && (year > 5);
isValid = isValid && (!string.IsNullOrEmpty(x.TradeType));
isValid = isValid && (x.Currency.Equals("GBP") || x.Currency.Equals("EUR") || x.Currency.Equals("USD") || x.Currency.Equals("INR"));
isValid = isValid && (!string.IsNullOrEmpty(x.Amount));
int i;
bool isnumeric = int.TryParse(x.Amount, out i);
isValid = isValid && (isnumeric);
}
if(isValid)
{
Trade val= new Trade();
val.TradeId= x.TradeId;
val.ISIN= x.ISIN;
val.TradeDate= x.TradeDate;
val.MaturityDate= x.MaturityDate;
val.SchemeName= x.SchemeName;
val.TradeType= x.TradeType;
val.Currency= x.Currency;
val.Amount= x.Amount;
validTrades.Add(val);
}
else
{
Trade valerror= new Trade();
valerror.TradeId= x.TradeId;
valerror.ISIN= x.ISIN;
valerror.TradeDate= x.TradeDate;
valerror.MaturityDate= x.MaturityDate;
valerror.SchemeName= x.SchemeName;
valerror.TradeType= x.TradeType;
valerror.Currency= x.Currency;
valerror.Amount= x.Amount;
invalidTrade.Add(valerror);
}
}
SaveInvalidRecordsToLogFile(invalidTrade, errorLogFilePath, errorLogFileName);
// SaveInvalidRecordsToLogFile(List<Trades>); // pass all the invalid trades to log...
return validTrades;
}
public bool SaveInvalidRecordsToLogFile(List<Trade> invalidTrades, string errorLogFilePath, string errorLogFileName)
{
//Do your logic here
//Do not hardcode the filename and the file path here
if (invalidTrades != null && invalidTrades.Count > 0)
{
string errorLogfile = errorLogFilePath + errorLogFileName;
try
{
if (!File.Exists(errorLogfile))
{
var invalidfile = File.Create(errorLogfile);
invalidfile.Close();
}
using (StreamWriter swinvalid = File.AppendText(errorLogfile))
{
swinvalid.WriteLine("TradeId|ISIN|TradeDate|MaturityDate|Tradetype|Currency|Amount");
foreach (Trade ivt in invalidTrades)
{
swinvalid.WriteLine(ivt.TradeId + "," + ivt.ISIN + "," + ivt.TradeDate + "," + ivt.MaturityDate + "'" + ivt.TradeType + "," + ivt.Currency + "," + ivt.Amount);
}
}
}
catch (Exception ex)
{
throw new FXCalculatorException(ex.Message);
}
}
return true;
}
public bool SaveValidRecordsToDB(List<Trade> validTrades, SqlConnection sqlConnectionObject)
{
//Do your logic here to upload to DB table
//Do not hardcode the connection string here
//Do not create the redundant connection Object for SqlConnection, use the conncetionObject given in the method parameter.
//Do not Prefix Database name in the SQL Query. Query should be "Insert into SBA.TableName"
//Should not be "Insert into DatabaseName.SBA.TableName"
//var ConnectionString = sqlConnectionObject.ConnectionString;
if (validTrades.Count > 0 && validTrades != null)
{
SqlConnection conn = sqlConnectionObject;
conn.Open();
foreach (Trade valid in validTrades)
{
SqlCommand cmd = new SqlCommand(("Insert into SBA.Trade_Details (TradeID,ISIN,TradeDate,MaturityDate,SchemeName,TradeType,Currency,Amount) values (#TradeID,#ISIN,#TradeDate,#MaturityDate,#SchemeName,#TradeType,#Currency,#Amount)"),conn);
cmd.Parameters.Add("#TradeID",valid.TradeId);
cmd.Parameters.Add("#ISIN", valid.ISIN);
cmd.Parameters.Add("#TradeDate", valid.TradeDate);
cmd.Parameters.Add("#MaturityDate", valid.MaturityDate);
cmd.Parameters.Add("#SchemeName", valid.SchemeName);
cmd.Parameters.Add("#TradeType", valid.TradeType);
cmd.Parameters.Add("#Currency", valid.Currency);
cmd.Parameters.Add("#Amount", valid.Amount);
cmd.ExecuteNonQuery();
}
conn.Close();
}
return true;
}
public List<FXRate> CalculateFXRate(SqlConnection sqlConnectionObject)
{
// TODO :Read the Trade details for TradeType FX from database and calculate the rates.
// Calculate the rate for each trade and add in a list of FXRates.
//Do not Prefix Database name in the SQL Query. Query should be "Insert into SBA.TableName"
//Should not be "Insert into DatabaseName.SBA.TableName"
//List<FXRate> FxRates = null; // assign list of FXRates;
//Do not hardcode the connection string here
//Do not create the redundant connection Object for SqlConnection, use the conncetionObject given in the method parameter.
List<FXRate> FxRates = new List<FXRate>();
List<Trade> trades = new List<Trade>();
try
{
SqlConnection conne = sqlConnectionObject;
string queryString = "Select * from SBA.Trade_Details";
SqlCommand cmd = new SqlCommand(queryString, conne);
conne.Open();
SqlDataReader datareader = cmd.ExecuteReader();
while (datareader.Read())
{
Trade validfx = new Trade{TradeId = datareader["TradeId"].ToString(),ISIN = datareader["ISIN"].ToString(),TradeDate = datareader["TradeDate"].ToString(),
MaturityDate = datareader["MaturityDate"].ToString(),SchemeName = datareader["SchemeName"].ToString(),TradeType = datareader["TradeType"].ToString(),
Currency = datareader["Currency"].ToString(), Amount = datareader["Amount"].ToString()};
trades.Add(validfx);
}
conne.Close();
foreach (Trade trad_para_to_calc_fx in trades)
{
FXRate fx = new FXRate();
fx.TradeId = trad_para_to_calc_fx.TradeId;
fx.Currency = trad_para_to_calc_fx.Currency;
fx.Amount = trad_para_to_calc_fx.Amount;
float amount = float.Parse(fx.Amount, CultureInfo.InvariantCulture.NumberFormat);
if (trad_para_to_calc_fx.Currency == "USD")
{
fx.AppliedFXRate = float.Parse("0.5",CultureInfo.InvariantCulture.NumberFormat).ToString();
float app_fx_rate = float.Parse("0.5",CultureInfo.InvariantCulture.NumberFormat);
fx.CalculatedFXRate = ((app_fx_rate) * (amount)).ToString();
}
if (trad_para_to_calc_fx.Currency == "GBP")
{
fx.AppliedFXRate = float.Parse("0.6", CultureInfo.InvariantCulture.NumberFormat).ToString();
float app_fx_rate = float.Parse("0.7",CultureInfo.InvariantCulture.NumberFormat);
fx.CalculatedFXRate = ((app_fx_rate) * (amount)).ToString();
}
if (trad_para_to_calc_fx.Currency == "EUR")
{
fx.AppliedFXRate = float.Parse("0.7", CultureInfo.InvariantCulture.NumberFormat).ToString();
float app_fx_rate = float.Parse("0.7",CultureInfo.InvariantCulture.NumberFormat);
fx.CalculatedFXRate = ((app_fx_rate) * (amount)).ToString();
}
if (trad_para_to_calc_fx.Currency == "INR")
{
fx.AppliedFXRate = float.Parse("1", CultureInfo.InvariantCulture.NumberFormat).ToString();
float app_fx_rate = float.Parse("1",CultureInfo.InvariantCulture.NumberFormat);
fx.CalculatedFXRate = ((app_fx_rate) * (amount)).ToString();
}
FxRates.Add(fx);
}
}
catch (Exception ex)
{
throw new FXCalculatorException(ex.Message);
}
return FxRates;
}
public bool SaveFXRate(List<FXRate> fxRates, SqlConnection sqlConnectionObject)
{
//Do your logic here to upload to DB table
//Do not hardcode the connection string here
//Do not create the redundant connection Object for SqlConnection, use the conncetionObject given in the method parameter.
//Do not Prefix Database name in the SQL Query. Query should be "Insert into SBA.TableName"
//Should not be "Insert into DatabaseName.SBA.TableName"
try
{
if (fxRates.Count > 0 && fxRates != null)
{
SqlConnection conne = sqlConnectionObject;
conne.Open();
foreach(FXRate calculated in fxRates)
{
SqlCommand cmd = new SqlCommand("Insert into SBA.FX_Rate (TradeId,Currency,Amount,AppliedFXRate,CalculatedFXRate) values (#TradeId,#Currency,#Amount,#AppliedFXRate,#CalculatedFXRate)", conne);
cmd.Parameters.AddWithValue("#TradeId", calculated.TradeId);
cmd.Parameters.AddWithValue("#Currency",calculated.Currency);
cmd.Parameters.AddWithValue("#Amount",calculated.Amount);
cmd.Parameters.AddWithValue("#AppliedFXRate",calculated.AppliedFXRate);
cmd.Parameters.AddWithValue("#CalculatedFXRate", calculated.CalculatedFXRate);
cmd.ExecuteNonQuery();
}
conne.Close();
}
}
catch (Exception ex)
{
throw new FXCalculatorException(ex.Message);
}
return true;
}
public bool CopyToArchive(string sourcePathWithFileName, string targetPathWithFileName)
{
//Do your logic here
//Do not hardcode the filename and the file path here
try
{
string inputpath="";
string input="";
FileInfo[] files;
DirectoryInfo Di;
string targetFile = sourcePathWithFileName + targetPathWithFileName;
Di = new DirectoryInfo(#"D:\frameworksample\");
files = Di.GetFiles("*.txt", SearchOption.AllDirectories);
foreach (FileInfo di1 in files)
{
if (di1.Name == "TradeOrders_032013.txt")
{
inputpath = di1.DirectoryName.ToString();
input = inputpath+"\\" + di1.Name.ToString();
}
}
if (!Directory.Exists(sourcePathWithFileName))
{
Directory.CreateDirectory(sourcePathWithFileName);
var targetfilecreation = File.Create(targetFile);
targetfilecreation.Close();
}
else
{
File.Delete(targetFile);
Directory.Delete(sourcePathWithFileName, true);
Directory.CreateDirectory(sourcePathWithFileName);
var targetfilecreation =File.Create(targetFile);
targetfilecreation.Close();
}
System.IO.File.Copy(input, targetFile, true);
}
catch (Exception ex)
{
throw new FXCalculatorException(ex.Message);
}
//File.Copy(sourcePathWithFileName + "\\" + targetPathWithFileName, true);
return true;
}
private void ProcessData(Main main)
{
throw new NotImplementedException();
}
//internal void ProcessData(string p, string p_2, string p_3, string p_4, System.Data.SqlClient.SqlConnection connectionObject, string p_5, string p_6)
//{
// throw new NotImplementedException();
//}
}
}

How to auto Increment a primarykey in JDBC sql

can you please guys help me, i'm having trouble on making my primary key into auto-increment, My table name is books and the column that i want to be auto-increment is serial_no which is a primary key.
public class donate extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
PreparedStatement pst;
DefaultTableModel loginModel = new DefaultTableModel();
int curRow = 0;
/**
* Creates new form donate
*/
public donate() {
initComponents();
DoConnect();
showAll();
}
void showAll(){
try{
rs = stmt.executeQuery("SELECT * FROM books");
while(rs.next())
{
String book = rs.getString("book_title");
String categorie = rs.getString("category");
String status = rs.getString("book_status");
String donators = rs.getString("donator");
int serial_nos = rs.getInt("serial_no");
loginModel.addRow(new Object[]{book, categorie, status, donators, serial_nos});
}
}catch(SQLException err){
System.out.println(err);
}
}
void DoConnect( ) {
try{
//CONNECT TO THE DATABASE
String host = "jdbc:derby://localhost:1527/Dafuq7";
String uName ="Dafuq7";
String uPass ="Dafuq7";
con = DriverManager.getConnection(host, uName, uPass);
//EXECUTE SOME SQL AND LOAD THE RECORDS INTO THE RESULTSET
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM books";
rs = stmt.executeQuery(sql);
}
catch(SQLException err){
JOptionPane.showMessageDialog(donate.this, err.getMessage());
}
}
and here is for may button, which when i input all the data will be submitted to my table books
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String bookttl = bookt.getText();
String yourn = yn.getText();
String categ = cat.getSelectedItem().toString();
String bstat = bs.getSelectedItem().toString();
try {
rs.moveToInsertRow();
rs.updateString( "book_title", bookttl );
rs.updateString( "category", yourn );
rs.updateString( "book_status", categ );
rs.updateString( "donator", bstat );
loginModel.addRow(new Object[]{bookttl, yourn, categ, bstat});
rs.insertRow( );
stmt.close();
rs.close();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM books";
rs = stmt.executeQuery(sql);
}
catch (SQLException err) {
System.out.println(err.getMessage() );
}// TODO add your handling code here:
}
BTW i found another way around by doing this, grabbing my table and reconstructing it and put this code in the create table script
SERIAL_NO INTEGER default AUTOINCREMENT: start 1 increment 1 not null primary key
Simply define your serial_no column as int primary key generated always as identity and then Derby will automatically assign the numbers for you. Here is some example code:
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(
"jdbc:derby:C:/__tmp/derbytest;create=true")) {
String sql;
sql = "DROP TABLE books";
try (Statement s = conn.createStatement()) {
s.executeUpdate(sql);
} catch (Exception e) {
// assume table did not previously exist
}
sql = "CREATE TABLE books (" +
"serial_no int primary key " +
"generated always as identity, " +
"title varchar(100))";
try (Statement s = conn.createStatement()) {
s.executeUpdate(sql);
}
sql = "INSERT INTO books (title) VALUES (?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, "The Book of Foo");
ps.executeUpdate();
ps.setString(1, "The Book of Bar");
ps.executeUpdate();
ps.setString(1, "The Book of Baz");
ps.executeUpdate();
}
sql = "SELECT * FROM books";
try (Statement s = conn.createStatement()) {
try (ResultSet rs = s.executeQuery(sql)) {
while (rs.next()) {
System.out.println(String.format(
"%d: %s",
rs.getInt("serial_no"),
rs.getString("title")));
}
}
}
} catch (SQLException se) {
se.printStackTrace(System.out);
System.exit(0);
}
}
which produces
1: The Book of Foo
2: The Book of Bar
3: The Book of Baz

Oracle columns update not happpening

I am trying to update a few columns in a Oracle table from my C# code.
Here is my method:
private static bool UpdateOracleTable(OracleTable table, string whereClause, List<int> entIDs)
{
try
{
var tableName = table.ToString();
using (OracleConnection conn = new OracleConnection(_oracleConnection))
{
conn.Open();
foreach (var id in entIDs)
{
whereClause = String.Format(whereClause, id);
var query = Resources.UpdateOracle;
query = String.Format(query, tableName, "20", DateTime.Now.ToString("yyyy/MM/dd"), whereClause);
using (OracleCommand cmd = new OracleCommand(query, conn))
{
cmd.ExecuteNonQuery();
}
}
}
return true;
}
catch (Exception ex)
{
Log.Debug(LogType.Error, ex);
return false;
}
}
Here is the Query:
UPDATE
{0}
SET
SYNC_STATUS = '{1}'
,SYNC_DATE = TO_DATE('{2}', 'yyyy/mm/dd')
{3}
And the where clause will look something like:
WHERE ID = {0}
This method updates about 10 records, and the rest stays null. This mehod does return true, and I have debugged, no exception is thrown.
Why does it not update all records?
This isn't an answer but might help debug the problem.
Instead of the like:
cmd.ExecuteNonQuery();
put in this:
int count = cmd.ExecuteNonQuery();
if (count == 0)
{
Console.WriteLine("");
}
Put a break on the Console.WriteLine("") and run it. The debugger will stop if no rows were updated. You can then check the query, and whether or not that ID actually exists.
The problem was with the WHERE clause. Since it contains a place holder {0}, after I I formatted the WHERE clause, the ID always stayed to the value it was formatted with first.
This is what my new method looks like.
private static bool UpdateOracleTable(OracleTable table, string whereClause, List<int> entIDs)
{
try
{
var tableName = table.ToString();
using (OracleConnection conn = new OracleConnection(_oracleConnection))
{
conn.Open();
foreach (var id in entIDs)
{
string originalWhere = whereClause;
originalWhere = String.Format(originalWhere, id);
var query = Resources.UpdateOracle;
query = String.Format(query, tableName, "20", DateTime.Now.ToString("yyyy/MM/dd"), originalWhere);
using (OracleCommand cmd = new OracleCommand(query, conn))
{
bool success = cmd.ExecuteNonQuery() > 0;
}
}
}
return true;
}
catch (Exception ex)
{
Log.Debug(LogType.Error, ex);
return false;
}
}
As can be seen, I added a variable 'originalWhere', that gets formatted, but most importantly, is being set to original WHERE clause parameter passed, so that it will always contain the place holder.

MS Access [Microsoft][ODBC Driver Manager] Invalid cursor state

I had the error in this code snippet:
private String[][] connectToDB(String query) throws ClassNotFoundException{
String[][] results = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String db = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:/EACA_AgroVentures1.accdb";
conn = DriverManager.getConnection(db);
stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery();
ResultSetMetaData rsm = rs.getMetaData();
rs.beforeFirst();
int columns = rsm.getColumnCount();
int rows = getRowCount(rs);
//int rows = rs.getFetchSize();
int rowCount = 0;
results = new String[rows][columns];
//System.out.println(rows+" "+columns);
while((rs!=null) && (rs.next())){
for(int i = 1; i < columns; i++){
results[rowCount][i-1] = rs.getString(i); // --> ERROR SHOWS HERE
//System.out.println(rowCount+","+i+" = "+rs.getString(i));
}
rowCount++;
}
rs.getStatement().close();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
return results;
}
My query consists of the following:
private void loadMR(){
try {
String query = "SELECT dealerCode, SUM(kg) AS totalKG, SUM(price) AS totalPrice, returnDate, BID FROM meatReturns GROUP BY BID, dealerCode, returnDate;";
Object[][] result = connectToDB(query);
// some more code below..
I tried using the first code with some other query given in another method:
private void loadDealers(){
try {
String query = "SELECT * FROM Dealers";
Object[][] result = connectToDBWithRows(
query);
// some more code..
and it runs perfectly well. What is going on here? How can i fix this problem?
UPDATE: the only difference of connectToDBWithRows and connectToDB is the while loop that manages the resultSet
// Snippet from connectToDBWithRows()
while((rs!=null) && (rs.next())){
for(int i = 0; i < columns; i++){
if (i == 0){
// Do nothing
}else{
results[rowCount][i] = rs.getString(i);
//System.out.println(rowCount+","+i+" = "+rs.getString(i));
}
}
rowCount++;
}
and this is my getRowCount() method
private int getRowCount(ResultSet resultSet){
int size = 0;
try {
resultSet.last();
size = resultSet.getRow();
resultSet.beforeFirst();
}
catch(Exception ex) {
return 0;
}
return size;
}
I've noticed that sometimes, Access needs you to specify the table name when referring to columns in sql statements. Try the following:
private void loadMR(){
try {
String query = "SELECT meatReturns.dealerCode, SUM(meatReturns.kg) AS totalKG, SUM(meatReturns.price) AS totalPrice, meatReturns.returnDate, meatReturns.BID FROM meatReturns GROUP BY meatReturns.BID, meatReturns.dealerCode, meatReturns.returnDate";
Object[][] result = connectToDBWithRows(query);

How to create a new table in a database with program that uses entity framework?

I have a piece of software out in the field and I need to add a new table to it. I have the table in my entity framework and new clients get the table. How to I update the others?
ADDED: To make it clear my development and new clients have the table. How to update the older clients databases is the question?
Since it is in my model it seems I should just call a create method and everything should happen under the hood.
_context.NewTable.CreateTable();
But think that I will have to write a sql command string to see if table exists and if it doesn't to create the table.
IDVisitorEntities _context = new IDVisitorEntities ();
String cmd = IF NOT EXISTS ( SELECT [name]
FROM sys.tables
WHERE [name] = NewTable )
CREATE TABLE NewTable (
ID int IDENITY,
NAME VARCHAR(40))
_context.NewTable.CommandText (cmd);
I want to only run this one time if the table doesn't exist. So that doesn't solve that problem. I really don't know what to do.
ADDED 5/6/2013
I'm thinking that EF has the Property Collection for each table and that might hold a clue. Might have to use ICustomTypeDescriptor ... Anyone else have any thoughts?
Added 7/15/2013
I started building it, at my new job, here is a sample. I need to create a file(s) with the partial classes and have the abstract and interface applied to them. It has a long way to go but this is a start...
namespace DataModelMAXFM
{
public abstract class ATable
{
private ArrayList _columns = new ArrayList();
private ArrayList colsToAdd = new ArrayList();
public ArrayList Columns
{
get
{
return _columns;
}
}
public bool TableCreate(SqlConnection sqlConn)
{
//assuming table needs to be created (already checked)
//get column list
//use for loop to create query string
// run command
ITable thisItable;
if (Columns.Count <= 0) //generate column list if not already created
{
if (!ColumnList())
return false;
}
if (this is ITable)
{
thisItable = (ITable) this;
}
else
{
throw new Exception("");
}
StringBuilder sb = new StringBuilder("CREATE TABLE " + thisItable.GetTableName() + " (");
bool flgFirst = true; // to allow for proper comma placement
foreach (PropertyInfo prop in Columns)
{
String propType = this.GetDataType(prop);
if (propType == String.Empty)//check to make sure datatype found a match, EF to SQL
{
return false;
}
if (!flgFirst)
{
sb.Append(", ");
}
else
{
flgFirst = false;
}
sb.Append(prop.Name + " " + propType);
}
// add right parentheses
sb.Append(")");
//now run query created above
SqlCommand com;
try
{
com = new SqlCommand(sb.ToString(), sqlConn);
com.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine("TableCreate e:" + e.ToString());
return false;
}
return true;
}
public bool TableExists(SqlConnection sqlConn)
{
SqlDataReader sdr = null;
SqlCommand com;
ITable thisItable;
try
{
//create and execute command
if (this is ITable)
thisItable = (ITable)this;
else
{
throw new Exception("");
}
com = new SqlCommand("Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = " + thisItable.GetTableName(), sqlConn);
sdr = com.ExecuteReader();
if (!sdr.HasRows)//ie table does not exist
{
return false;
}
}
catch (Exception e)
{
Console.WriteLine("TableCreate e:" + e.ToString());
return false;
}
//close datareader
try
{
sdr.Close();
}
catch (Exception e)
{
Console.WriteLine("close sqldatareader TableExists e: " + e.ToString());
}
return true;
}
public bool ColumnList()
{
bool flgListCreated = false;
PropertyInfo[] propList = typeof(TransactionCategory).GetProperties();
foreach (PropertyInfo prop in propList)
{
if (prop.CanRead && prop.CanWrite)
{
MethodInfo mget = prop.GetGetMethod(false);
MethodInfo mset = prop.GetSetMethod(false);
// Get and set methods have to be public
if (mget == null)
{
continue;
}
if (mset == null)
{
continue;
}
Columns.Add(prop);
if (!flgListCreated)
{
flgListCreated = true;
}
}
}
return flgListCreated;
}
public bool ColumnsExist(SqlConnection sqlConn)
{
ITable thisItable;
if (Columns.Count <= 0)
{
if (!ColumnList())
return false;
}
//2013-07-10 create basic connection and data reader
if (this is ITable)
thisItable = (ITable)this;
else
{
throw new Exception("");
}
SqlDataReader sdr = null;
SqlCommand com;
foreach (PropertyInfo prop in Columns)
{
try
{
//create and execute command
com = new SqlCommand("Select * from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = " + thisItable.GetTableName() + " and COLUMN_NAME = " + prop.Name, sqlConn);
sdr = com.ExecuteReader();
//if no rows returned to datareader == column does not exist, add to ArrayList of columns to add
if (!sdr.HasRows)
colsToAdd.Add(prop);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
}
//close datareader
try
{
sdr.Close();
}
catch (Exception e)
{
Console.WriteLine("close sqldatareader ColumnsExist e: " + e.ToString());
}
if (colsToAdd.Count == 0)
return false;
//returns true only if method worked and found columns to add to DB
return true;
}
public bool ColumnsCreate(SqlConnection sqlConn)
{
ITable thisItable;
StringBuilder sb = new StringBuilder();
if (colsToAdd.Count <= 0)
{
if (!ColumnsExist(sqlConn)) //2013-07-08 - MAXIMUS\58398(BJH) //if no columns, attempt to create list
return false; // if Column list was not created, return false
}
// add a array of the alter table
if (this is ITable)
thisItable = (ITable)this;
else
{
throw new Exception();
}
sb.Append("ALTER TABLE " + thisItable.GetTableName() + " ADD ( ");
bool flgFirst = true; // allows for no leading comma on first entry
String propType;
foreach (PropertyInfo prop in colsToAdd)
{
//make sure SQL datatype can be determined from EF data
propType = this.GetDataType(prop);
if (propType == String.Empty)
throw new Exception("no datatype match found " + prop.Name + " " + prop.PropertyType.ToString());
if (!flgFirst)
{
sb.Append(", ");
}
else
{
flgFirst = false;
}
sb.Append(prop.Name + " " + propType);
}
sb.Append(" )");
SqlCommand com;
try
{
com = new SqlCommand(sb.ToString(), sqlConn);
com.ExecuteNonQuery();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return false;
}
return true;
}
public bool ColumnsUpdate(SqlConnection sqlConn)
{
if (ColumnsExist(sqlConn))
return ColumnsCreate(sqlConn);
else
return false;
}
//method to convert from EF to SQL datatypes, see noted issues
public String GetDataType(PropertyInfo pi)
{
String s = "";
String pistr = pi.PropertyType.ToString();
switch (pistr)
{
case "Byte[]":
s = "binary";
break;
case "Boolean":
s = "bit";
break;
case "String Char[]": // also maps to other options such as nchar, ntext, nvarchar, text, varchar
s = "char";
break;
case "DateTime":
s = "datetime";
break;
case "DateTimeOffset":
s = "datetimeoffset";
break;
case "Decimal":
s = "decimal";
break;
case "Double":
s = "float";
break;
case "Int16":
s = "smallint";
break;
case "Int32":
s = "int";
break;
case "Int64":
s = "bigint";
break;
case "Single":
s = "real";
break;
case "TimeSpan":
s = "time";
break;
case "Byte":
s = "tinyint";
break;
case "Guid":
s = "uniqueidentifier";
break;
case "Xml":
s = "xml";
break;
default:
Console.WriteLine("No datatype match found for " + pi.ToString() + " " + pi.PropertyType.ToString());
return String.Empty;
}
return s;
}
}
public interface ITable
{
int ID
{
get;
}
bool TableUpdate(SqlConnection sqlConn);
bool TableStoredProceduresCreate(SqlConnection sqlConn);
bool TableStoredProceduresDrop(SqlConnection sqlConn);
bool TableCreateTriggers(SqlConnection sqlConn);
bool TableCreateViews(SqlConnection sqlConn);
DataTable GetDataTable();
DateTime dtTableImplemented
{
get;
}
String GetTableName();
}
}
How to I update the others?
And how do you upgrade code of others to require that table? Probably by using some patch or upgrade package and this package should create your table as well.
Since it is in my model it seems I should just call a create method and everything should happen under the hood.
No. EF itself has nothing to define database schema (Except creating SQL script for whole database creation). What you are looking for is possible with EF Migrations but that is feature of EF 4.3 and newer when using code first and DbContext API.